Tuesday, February 4, 2014

Re: Help saving data with CakePHP 3.0 ORM

Also check the array:
$data = ['name' => 'Walter White'];

Should be:
$data = array('name' => 'Walter White');


best regards.


2014-02-04 José Lorenzo <jose.zap@gmail.com>:
Currently that is the most confusing part of the ORM and I'd like to make it easier to understand in the next release preview. By default entities are protected agains mass assignment, this means that you cannot create them without configuring what properties can be set from the request. You need to create the entity class and set the properties that are accessible for mass assignment. Please read this section in the book:

http://book.cakephp.org/3.0/en/orm/entities.html#mass-assignment


On Tuesday, February 4, 2014 5:16:31 PM UTC+1, bato wrote:
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?

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.



--
Carlos Baeza Negroni
+56985644026
@cjbaezilla

--
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: