SHOW:
|
|
- or go back to the newest paste.
1 | MODEL: | |
2 | class TableasTable extends Table | |
3 | { | |
4 | /** | |
5 | * Initialize method | |
6 | * | |
7 | * @param array $config The configuration for the Table. | |
8 | * @return void | |
9 | */ | |
10 | public function initialize(array $config) | |
11 | { | |
12 | parent::initialize($config); | |
13 | ||
14 | $this->setTable('tableas'); | |
15 | $this->setDisplayField('name'); | |
16 | $this->setPrimaryKey('id'); | |
17 | ||
18 | $this->addBehavior('Timestamp'); | |
19 | ||
20 | $this->hasMany('tablebs', [ | |
21 | 'foreignKey' => 'tablea_id', | |
22 | 'bindingKey' => 'id', | |
23 | ]); | |
24 | } | |
25 | ||
26 | /** | |
27 | * Default validation rules. | |
28 | * | |
29 | * @param \Cake\Validation\Validator $validator Validator instance. | |
30 | * @return \Cake\Validation\Validator | |
31 | */ | |
32 | public function validationDefault(Validator $validator) | |
33 | { | |
34 | $validator | |
35 | ->integer('id') | |
36 | ->allowEmptyString('id', null, 'create'); | |
37 | ||
38 | $validator | |
39 | ->scalar('name') | |
40 | ->maxLength('name', 22) | |
41 | ->requirePresence('name', 'create') | |
42 | ->notEmptyString('name'); | |
43 | ||
44 | return $validator; | |
45 | } | |
46 | } | |
47 | ||
48 | Query: | |
49 | $ordersTable = TableRegistry::getTableLocator()->get('Tableas'); | |
50 | - | $q = $ordersTable->find('all',['contain'=>['Tablebs']])->execute();//->where(['UnasOrders.id' => '40238-114081']); |
50 | + | $q = $ordersTable->find('all',['contain'=>['Tablebs']])->execute(); |
51 | - | // $q = $ordersTable->find('all')->contain('UnasOrdersItems'); |
51 | + | print_r($q); |
52 | - | print_r($q); |
52 | + | |
53 | [queryString] => SELECT Tableas.id AS `Tableas__id`, Tableas.name AS `Tableas__name`, Tableas.modified AS `Tableas__modified`, Tableas.created AS `Tableas__created` FROM tableas Tableas | |
54 | ||
55 | If I change the model relation to hasOne (the query code is left as is), the generated querystring becomes: | |
56 | ||
57 | [queryString] => SELECT Tableas.id AS `Tableas__id`, Tableas.name AS `Tableas__name`, Tableas.modified AS `Tableas__modified`, Tableas.created AS `Tableas__created`, tablebs.id AS `tablebs__id`, tablebs.tablea_id AS `tablebs__tablea_id`, tablebs.name AS `tablebs__name`, tablebs.created AS `tablebs__created`, tablebs.modified AS `tablebs__modified` FROM tableas Tableas LEFT JOIN tablebs tablebs ON Tableas.id = (tablebs.tablea_id) | |
58 | ||
59 | ||
60 | What am I missing/ doing wrong in case of the hasMany relation? |