Wednesday, March 12, 2014

Re: V3.0 - saving fails...

Add the Timestamp behavior to your table:

class ArticlesTable extends Table {

  public function initialize(array $config) {
     $this->addBehavior('Timestamp');
  }
}

http://book.cakephp.org/3.0/en/core-libraries/behaviors/timestamp.html


On Wednesday, March 12, 2014 11:10:05 AM UTC+1, Dr. Tarique Sani wrote:
Thanks again. Last question of the day - what to do to get created and modified fields to work automagically. Created can be defaulted to current timestamp, what about modified?

Tarique


On Wed, Mar 12, 2014 at 3:01 PM, José Lorenzo <jose...@gmail.com> wrote:
Entities go in App/Model/Entity/ and they should be the singular of your table without suffixes. For example App/Model/Entity/Article.php that way cakephp will be able to load it and know it is associated to ArticlesTable.

If you wish to divert from the conventions you can change it in your table class:

class ArticlesTable extends Table {

  public function initialize(array $config) {
     $this->entityClass('App\Model\ArticleEntity');
  }
}

In general I would not recommend this as it makes it harder for the FormHelper to figure out where stuff is.


On Wednesday, March 12, 2014 10:12:30 AM UTC+1, Dr. Tarique Sani wrote:
ok that brings me to the next question. 

How do I get $article = $this->Articles->newEntity($this->request->data); to use the Entity class I have created in  App/Model/ArticleEntity.php ? Doesn't the controller automagically include it, What is the convention for Entity classes?

Tarique



On Wed, Mar 12, 2014 at 12:53 PM, José Lorenzo <jose...@gmail.com> wrote:
This could possibly be the most difficult part to understand in 3.0 and may be subject to change if it turns out to be too complicated. All properties in the entities are protected by default against mass assignment, you need to modify your Article entity in order to let CakePHP know what properties can be assigned by newEntity:

class Article extends Entity {

  protected $_accessible = ['title' => true, 'body' => true];

}

or if you just don't care (not recommended):

class Article extends Entity {

  protected $_accessible = ['*' => true];

}

The latter can be seen as a Eloquent::unguard();

On Wednesday, March 12, 2014 6:30:45 AM UTC+1, Dr. Tarique Sani wrote:
Wonder why saving is failing 

I am doing 

$article = $this->Articles->newEntity($this->request->data);

if ($this->Articles->save($article)) {
                $this->Session->setFlash(__('Your article has been saved.'));
                return $this->redirect(['action' => 'index']);
}

$this->Session->setFlash(__('Unable to add your article.'));


debug($this->request->data);

gives

[
'title' => 'Test Title',
'body' => 'This is the body'
]


but debug($article);

give

object(Cake\ORM\Entity) {

'new' => null,
'accessible' => [],
'properties' => [],
'dirty' => [],
'virtual' => [],
'errors' => []

}

What can possibly be wrong. I am guessing the title and body should appear as properties of the newly created entity without which the saving will not happen

I did a composer update just now - so I do have the latest code.

Cheers
Tarique

--
=============================================================
The Conference Schedule Creator : http://shdlr.com

PHP for E-Biz : http://sanisoft.com
=============================================================

--
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+u...@googlegroups.com.
To post to this group, send email to cake...@googlegroups.com.

Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.



--
=============================================================
The Conference Schedule Creator : http://shdlr.com

PHP for E-Biz : http://sanisoft.com
=============================================================

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



--
=============================================================
The Conference Schedule Creator : http://shdlr.com

PHP for E-Biz : http://sanisoft.com
=============================================================

--
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/d/optout.

No comments: