Tuesday, February 4, 2014

Help saving data with CakePHP 3.0 ORM

Hi,

I'm starting to play with 3.0 ORM and I encountered some issues saving data.

I have two simple tables: 
  • authors with fields id and name
  • posts with fields id, title, text and author_id

I have fill tables with two authors and some posts by hand to try retrieving data with cake.

I have set up two Table class as:

App/Model/Table/AuhtorsTable.php

<?php

namespace App\Model\Table;

use Cake\ORM\Table;

class AuthorsTable extends Table {

    public function initialize(array $config) {
        $this->hasMany('Posts');
    }

}

and 

App/Model/Table/PostsTable.php

<?php

namespace App\Model\Table;

use Cake\ORM\Table;

class PostsTable extends Table {

    public function initialize(array $config) {
        $this->belongsTo('Authors');
    }

}

In App/Controller/PagesController.php I'm trying to save data from an array.

$data = ['name' => 'Walter White'];
$authors = TableRegistry::get('Authors');
$author = $authors->newEntity($data);
$authors->save($author);

The data aren't saved. The save method return false but I don't understand why.
Am I missing something?
I have following http://book.cakephp.org/3.0/en/orm/table-objects.html#converting-request-data-into-entities

best regards
alberto




--
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 http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: