我有一个简单的问题,但我无法在任何地方找到答案 - 无论是在文档中还是在本论坛中。
简而言之 - 我在Kohana 3中有2个模型 - 任务和任务类型。我想以这样的方式创建一个Task模型,然后我可以引用$ task-> task_type_id-> name字段。
不幸的是,当您将记录插入数据库时,我得到答案:“常规错误:1364字段'task_type_id'没有默认值”
当我将task_type_id字段添加为普通字段(在$ _table_columns中)时,我无法通过$ task-> task_type_id-> name引用视图中的TaskType对象。
我不知道如何配置模型以保存与数据库的关系,并能够在视图中引用它。
请帮忙。
我的型号代码:
class Model_Task extends ORM
{
protected $_table_name = 'task';
protected $_table_columns = array(
'id'=>array('data_type'=>'int','is_nullable'=>false),
'date' => array('data_type'=>'date','is_nullable'=>
'description'=>array('data_type'=>'varchar','is_nullable'=>true)
);
protected $_belongs_to = array(
'task_type_id' => array(
'model' => 'TaskType',
'foreign_key' => 'id'
)
);
}
class Model_TaskType extends ORM
{
protected $_table_name = 'task_type';
protected $_table_columns = array(
'id'=>array('data_type'=>'int','is_nullable'=>false),
'name' => array('data_type'=>'string','is_nullable'=>false)
);
}
可能它应该是:
class Model_Task extends ORM
{
protected $_table_name = 'task';
protected $_table_columns = array(
'id'=>array('data_type'=>'int','is_nullable'=>false),
'date' => array('data_type'=>'date','is_nullable'=>
'description'=>array('data_type'=>'varchar','is_nullable'=>true),
'task_type_id' => array('data_type'=>'int','is_nullable'=>false),
);
protected $_belongs_to = array(
'task_type' => array(
'model' => 'TaskType',
'foreign_key' => 'id'
)
);
}
class Model_TaskType extends ORM
{
protected $_table_name = 'task_type';
protected $_table_columns = array(
'id'=>array('data_type'=>'int','is_nullable'=>false),
'name' => array('data_type'=>'string','is_nullable'=>false)
);
}
并由$task->add('task_type', $task_type);
添加