Kohana 3 ORM:保存关系
<?php
class Model_Cliente extends ORM {
protected $_table_name = 'clientes';
protected $_primary_key = 'id';
protected $_has_one = array('loja' => array());
}
?>
<?php
class Model_Loja extends ORM {
protected $_table_name = 'lojas';
protected $_primary_key = 'id';
}
?>
<?php
// code here ...
public function action_index()
{
$loja = ORM::Factory('loja');
$loja->nome = 'Foo Bar Store';
$loja->endereco = 'foobar';
$loja->senha = '123456';
$cliente = ORM::Factory('cliente');
$cliente->empresa = 'Foo Bar Ltda.';
$cliente->responsavel = 'João da Silva Foo Bar';
$cliente->email = '[email protected]';
$cliente->telefone = '123456789';
$cliente->novidades = true;
$cliente->loja = $loja;
$cliente->save();
}
// code here ...
?>
我收到错误:“Kohana_Exception [ 0 ]:Loja 属性在 Model_Cliente 类中不存在”。
为什么我不能将 loja 添加到 cliente ($cliente->loja = $loja) 并保存?
<?php
class Model_Cliente extends ORM {
protected $_table_name = 'clientes';
protected $_primary_key = 'id';
protected $_has_one = array('loja' => array());
}
?>
<?php
class Model_Loja extends ORM {
protected $_table_name = 'lojas';
protected $_primary_key = 'id';
}
?>
<?php
// code here ...
public function action_index()
{
$loja = ORM::Factory('loja');
$loja->nome = 'Foo Bar Store';
$loja->endereco = 'foobar';
$loja->senha = '123456';
$cliente = ORM::Factory('cliente');
$cliente->empresa = 'Foo Bar Ltda.';
$cliente->responsavel = 'João da Silva Foo Bar';
$cliente->email = '[email protected]';
$cliente->telefone = '123456789';
$cliente->novidades = true;
$cliente->loja = $loja;
$cliente->save();
}
// code here ...
?>
I get the error: "Kohana_Exception [ 0 ]: The loja property does not exist in the Model_Cliente class".
Why cannot I add loja to cliente ($cliente->loja = $loja) and save then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ORM模型只能设置
belongs_to
关系。因此,您应该重新排序代码:并且不要忘记在
Model_Loja
中定义$_belongs_to
属性!ORM model can set only
belongs_to
relations. So, you should reorder your code:And dont forget to define
$_belongs_to
property inModel_Loja
!