# Model Table Problem

**URL:** <https://discourse.cakephp.org/t/model-table-problem/870>\
**Category:** Need Help\
**Created:** [August 2, 2016, 11:46am UTC](https://discourse.cakephp.org/t/model-table-problem/870 "2016-08-02T11:46:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Klaus](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/klaus/32/269_2.png) [@Klaus](https://discourse.cakephp.org/u/Klaus)\
**Post date:** [August 2, 2016, 11:46am UTC](https://discourse.cakephp.org/t/model-table-problem/870/1 "2016-08-02T11:46:43Z")

</div>

Hi all,  
I am just develloping an app where a table ‘offices’ exsists.  
I have baked the controller, Model and Views. Now it turns out, I can store new data but the data is not retreived from mysql. There is no error displayed and the app is not interrupted. No info for identifying the error.  
I have installed the latest cakephp version.  
Any idea where I can start making more research?

This is the code on OfficesTable.php:  
class OfficesTable extends Table  
{

```
/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('offices');
    $this->displayField('id');
    $this->primaryKey('id');

    $this->belongsTo('Cities', [
        'foreignKey' => 'city_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Persons', [
        'foreignKey' => 'person_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Events', [
        'foreignKey' => 'office_id'
    ]);
}

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator Validator instance.
 * @return \Cake\Validation\Validator
 */
public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->requirePresence('office', 'create')
        ->notEmpty('office');

    $validator
        ->requirePresence('street', 'create')
        ->notEmpty('street');

    $validator
        ->requirePresence('no', 'create')
        ->notEmpty('no');

    $validator
        ->requirePresence('phone', 'create')
        ->notEmpty('phone');

    return $validator;
}

/**
 * Returns a rules checker object that will be used for validating
 * application integrity.
 *
 * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
 * @return \Cake\ORM\RulesChecker
 */
public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->isUnique(['email']));
    $rules->add($rules->existsIn(['city_id'], 'Cities'));
    $rules->add($rules->existsIn(['person_id'], 'Persons'));

    return $rules;
}

```

}

---

<div class="post-metadata">

**Author:** ![mtancoigne](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mtancoigne/32/341_2.png) [@mtancoigne](https://discourse.cakephp.org/u/mtancoigne)\
**Post date:** [August 2, 2016, 11:48am UTC](https://discourse.cakephp.org/t/model-table-problem/870/2 "2016-08-02T11:48:50Z")

</div>

Have you checked the content of the table in your database ?

---

<div class="post-metadata">

**Author:** ![Klaus](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/klaus/32/269_2.png) [@Klaus](https://discourse.cakephp.org/u/Klaus)\
**Post date:** [August 2, 2016, 12:13pm UTC](https://discourse.cakephp.org/t/model-table-problem/870/3 "2016-08-02T12:13:10Z")

</div>

Hi, yes I have done. And now I am a step forward. I have entere a number in the field person\_id and the row is diplayed. The problem is the ‘belongsTo’ option. I’m going to check a better solution.

---

<div class="post-metadata">

**Author:** ![mtancoigne](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mtancoigne/32/341_2.png) [@mtancoigne](https://discourse.cakephp.org/u/mtancoigne)\
**Post date:** [August 2, 2016, 12:14pm UTC](https://discourse.cakephp.org/t/model-table-problem/870/4 "2016-08-02T12:14:54Z")

</div>

Is your id field in autoincrement ?

---

<div class="post-metadata">

**Author:** ![Klaus](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/klaus/32/269_2.png) [@Klaus](https://discourse.cakephp.org/u/Klaus)\
**Post date:** [August 2, 2016, 12:16pm UTC](https://discourse.cakephp.org/t/model-table-problem/870/5 "2016-08-02T12:16:35Z")

</div>

But by the way: Do you know cake is sometimes is offering only the id and sometimes the fullname of a table with two columns? Like : Cities with columns id and name. When one uses in a form as options.  
Regards Klaus

---

<div class="post-metadata">

**Author:** ![mtancoigne](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mtancoigne/32/341_2.png) [@mtancoigne](https://discourse.cakephp.org/u/mtancoigne)\
**Post date:** [August 2, 2016, 12:21pm UTC](https://discourse.cakephp.org/t/model-table-problem/870/6 "2016-08-02T12:21:45Z")

</div>

That’s weird, but maybe it comes from your table structure… I remember issues with no autoincrement fields on numeric PKs or with varchar PKs.

If you use the ‘cities’ table in a relation with another one, if the [cities.id](http://cities.id) is required but cake can’t automatically determine it, the ‘id’ field will be present in the forms of related content.

And maybe you updated the table and forget to re bake/build all the MVC…

---

<div class="post-metadata">

**Author:** ![Klaus](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/klaus/32/269_2.png) [@Klaus](https://discourse.cakephp.org/u/Klaus)\
**Post date:** [August 2, 2016, 12:33pm UTC](https://discourse.cakephp.org/t/model-table-problem/870/7 "2016-08-02T12:33:23Z")

</div>

Thanks for your ideas. I will follow up later - now I have a meeting. Regards

Hi, stupid. It was the case of ‘displayField’.  
And the first issue was related to a wrong value in a ‘belongsTo-Relation’.  
No further input needed. How do I close this post?
