Thursday, January 7, 2016

CakehasM 3 hasMany -> always empty

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 <bogdan.soos@external.gdfsuez.com>
*
* @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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: