Quantcast

CakehasM 3 hasMany -> always empty

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

CakehasM 3 hasMany -> always empty

Bogdan Soos
Hi there,

I am having a problem with hasMany.
Here is my case:

Models:
ContractTable -> hasMany -> CustomValues


<?php
/**
* Contract Model
*
* @property \Pilotage\NORM\Association\BelongsTo $Agents
* @property \Pilotage\NORM\Association\BelongsTo $Plateau
*/
class ContractTable extends Table
{

/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('offresmop_contratclt');
$this->displayField('cleaffaires');
$this->primaryKey('cleaffaires');
$this->addBehavior('Timestamp');
$this->belongsTo('LastAgent', [
'className' => 'Application.Agents',
'foreignKey' => 'last_mod_agent_id',
'joinType' => 'INNER'
]);
$this->belongsTo('ValidAgent', [
'className' => 'Application.Agents',
'foreignKey' => 'validator_agent_id'
]);
$this->belongsTo('FirstAgent', [
'className' => 'Application.Agents',
'foreignKey' => 'first_agent_id'
]);
$this->belongsTo('Plateau', [
'className' => 'Application.Plateau',
'foreignKey' => 'plateau_id',
'joinType' => 'INNER'
]);

$this->belongsTo('Product', [
'className' => 'Tranquillity.Products',
'foreignKey' => 'idproduit',
'joinType' => 'INNER'
]);

$this->belongsTo('Client', [
'className' => 'Tranquillity.Affaires',
'foreignKey' => 'bp',
'propertyName' => 'client',
'joinType' => 'INNER'
]);

$this->belongsTo('StatusRetour', [
'className' => StatusTable::class,
'foreignKey' => 'etatetur',
'propertyName' => 'status_retur',
'joinType' => 'INNER'
]);

$this->belongsTo('Status', [
'className' => StatusTable::class,
'foreignKey' => 'etat',
'propertyName' => 'status',
'joinType' => 'INNER'
]);


$this->hasMany('MetaValues', [
'className' => MetaValuesTable::class,
'foreignKey' => 'cleaffaire',
'bindingKey' => 'cleaffaires',
'strategy' => 'subquery',
'joinType' => 'LEFT'
]);
}
/** Validation and stuff */



/**
* Function findWithMetaValues - Add Meta Values to contract
*
* @author Bogdan SOOS <[hidden email]>
*
* @param Query $query
* @return $this|array
*/
public function findWithMetaValues(Query $query) {
return $query
->contain('MetaValues');
}
}


MetaValuesTable.php

<?php

/**
* MetaValues Model
*
*/
class MetaValuesTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('offresmop_meta_values');
$this->displayField('cleaffaire');
$this->primaryKey('id');
$this->addBehavior('Timestamp');

$this->belongsTo('Contract', [
'className' => 'Tranquillity.Contract',
'foreignKey' => 'cleaffaires',
'bindingKey' => 'cleaffaire'
]);
}

/**
* Default validation rules.
*
* @param \Pilotage\Validation\Validator $validator Validator instance.
* @return \Pilotage\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create');

$validator
->add('id_field', 'valid', ['rule' => 'numeric'])
->requirePresence('id_field', 'create')
->notEmpty('id_field');

$validator
->add('cleaffaire', 'valid', ['rule' => 'numeric'])
->requirePresence('cleaffaire', 'create')
->notEmpty('cleaffaire');

$validator
->allowEmpty('value');

return $validator;
}
}


Controller
$oContract =
$this->Contract
->find('withClient')
->where(['cleaffaires' => $this->request->data('cleaffaire')])
->first();
$oContract->meta_values <======== EMPTY
OR

$oContracts =
$this->Contract
->find('withClient')
->where(['cleaffaires' => $this->request->data('cleaffaire')]);

foreach($oContracts as $oContract){

$oContract->meta_values <======== EMPTY
}


My problem is that all my hasMany properties are empty. I saw the query being created in but the results are NEVER populated.

What am I doing wrong ? or how do I get my data ?


Thanks,
Bogdan.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: CakehasM 3 hasMany -> always empty

John Andersen
Hi Bogdan

Is the below correct? To me it does not seem correct :)
 $this->belongsTo('Contract', [
'className' => 'Tranquillity.Contract',

Enjoy, John

On Thursday, 7 January 2016 13:01:30 UTC+2, Bogdan Soos wrote:
Hi there,

I am having a problem with hasMany.
Here is my case:

Models:
ContractTable -> hasMany -> CustomValues

[snip]

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: CakehasM 3 hasMany -> always empty

Bogdan Soos
Hi John,

Thank you for your answer .

It's correct, because the model are in plugin called Tranquillity:) it gets the class, the entity, everything seems to be where it should... Just the data in the property is missing.

Bogdan

Sent from my iPhone

On 07 Jan 2016, at 19:07, John Andersen <[hidden email]> wrote:

Hi Bogdan

Is the below correct? To me it does not seem correct :)
 $this->belongsTo('Contract', [
'className' => 'Tranquillity.Contract',

Enjoy, John

On Thursday, 7 January 2016 13:01:30 UTC+2, Bogdan Soos wrote:
Hi there,

I am having a problem with hasMany.
Here is my case:

Models:
ContractTable -> hasMany -> CustomValues

[snip]

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/Yc0iKP6l7fk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Loading...