Hi.
I've got problem with loading related data again. I've got relation has_many/belongs to with News and Comments models.
Everything was ok, until I've changed method with loading news
from
news/show/5
to
news/show/news_title
where news title is content from column called "link".
I'm calling news now with this code:
$this->news =& $this->News->findFirst('link= ?',$this->params['title']);
if(!$this->news){
$this->news = $this->News->findFirst('link= 404');
}
...and now, I'm unable to load comments ( $news->comments outputs empty array)
I'm loading news via:
$News->comment->load();
Now, here's some hints:
News model:
var $has_many = 'comments';
Comments model:
var $belongs_to = array('news'=>array('primary_key_name' => 'id','foreign_key'=>'news_id'));
Now, I've run migrate news install successfully, But comment installer:
function up_1()
{
$this->createTable('comments', "
id,
created_at,
updated_at,
news_id,
body,
author
");
Ak::import('news,comment');
$news = new News(1);
$news->comment->create(array('body'=>'test','author'=>'krzyzak' ) );
$news->save();
}
outputs error like this:
http://wklej.org/id/bec40e4a79
How can I fix it?
PHP Notice: Table comments already exists on the database in /Applications/MAMP/htdocs/f1live/lib/AkInstaller.php on line 252
Notice: Table comments already exists on the database in /Applications/MAMP/htdocs/f1live/lib/AkInstaller.php on line 252
The table already exists. You should drop it first (and delete the file in /versions/)
And might the rollback be initiated because there already is a news record with ID 1 ?
well, I know, that table already exists- It's only a notice (with dropped table error is the same)
Indeed, record already exists- I'm creating comment to news with id 1.
As You propably saw, that news is selected, then it's created comment- if news doesn't exists, migration script outputs error much faster
(mysqlt): SELECT * FROM news WHERE news.id = 1 LIMIT 1
PHP Notice: Undefined property: News::$comment in /Applications/MAMP/htdocs/f1live/app/installers/comment_installer.php on line 24
Notice: Undefined property: News::$comment in /Applications/MAMP/htdocs/f1live/app/installers/comment_installer.php on line 24
PHP Fatal error: Call to a member function create() on a non-object in /Applications/MAMP/htdocs/f1live/app/installers/comment_installer.php on line 24
Fatal error: Call to a member function create() on a non-object in /Applications/MAMP/htdocs/f1live/app/installers/comment_installer.php on line 24
it's on 18 line of this http://wklej.org/id/bec40e4a79 log(when news with id 1 doesn't exists)
Well, I'm not sure, but I think that by using
$news = new News(1);
you are trying to create a new news object with a primary key of 1, instead of getting the existing record.
And that
(mysqlt): SELECT * FROM news WHERE news.id = 1 LIMIT 1
is a query Akelos does to see if the record already exists or not. (because the primary key has to be unique of course)
But no, wait.. I know what the problem is: You are using create() to add an associated record (the comment), but you should use build()
$news->comment->build(array('body'=>'test','author'=>'krzyzak' ) );
btw, just curious: what's the language on www.wklej.org ? Czech?
Have you tried?
if(!$news->save()){
print_r($news->getErrors());
}
ok, $news->getErrors() shows me error- summary column couldn't be blank- now, I've got comment, but I can't load them....
btw, just curious: what's the language on www.wklej.org ? Czech?
It's polish :)
1 to 7 of 7