Im-tring在自定义php应用程序中使用糖数据库结构。
例如,你会写一个类似于的查询
SELECT *
FROM quotes
INNER JOIN quotes_cstm on quotes.id = quotes_cstm.id_c AND quotes.deleted = 0
# some wheres here
这是我代码的一部分。
class quotes_cstmModel extends baseModel
{
}
class quotesModel extends baseModel
{
public static $select = ['id', 'name'];
protected $with = ['quotes_cstm'];
public function quotes_cstm()
{
return $this->hasOne('quotes_cstmModel','id_c');
}
}
报价模型的toArray输出类似于
array(
id => '231312',
name => 'quote name',
quotes_cstm = array(
'id_c' => '231312'
'some_cstm_field_c' => 'some value',
)
);
那么我要问的是,如何将id_c和some_cstm_field_c附加到主数组中,而不包含多维数组的quotes_cstm部分?
用法示例我想要类似的东西
我试着保持我对这种东西的习惯。
$quote = quotesModel::find('QUOTE_ID_HERE');
如果在引号中找不到__get方法,您可以重写该方法,以便尝试访问关系上的属性:
class quotesModel extends baseModel
{
public function __get($key)
{
return $this->getAttribute($key) ?: $this->quotes_cstm->getAttribute($key);
}
}