Kohana 3 ORM:保存关系

发布于 2024-10-07 12:32:57 字数 1061 浏览 4 评论 0原文

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2024-10-14 12:32:57

ORM模型只能设置belongs_to关系。因此,您应该重新排序代码:

$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->save();  // save parent model before linking

$loja = ORM::Factory('loja');
$loja->nome = 'Foo Bar Store';
$loja->endereco = 'foobar';
$loja->senha = '123456';
$loja->cliente = $cliente; // the same as $loja->cliente_id = $client->id;

$loja->save();

并且不要忘记在 Model_Loja 中定义 $_belongs_to 属性!

ORM model can set only belongs_to relations. So, you should reorder your code:

$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->save();  // save parent model before linking

$loja = ORM::Factory('loja');
$loja->nome = 'Foo Bar Store';
$loja->endereco = 'foobar';
$loja->senha = '123456';
$loja->cliente = $cliente; // the same as $loja->cliente_id = $client->id;

$loja->save();

And dont forget to define $_belongs_to property in Model_Loja!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文