我得到了一些“有用”的东西,如果有更好的方法,请告诉我。
1)使sql创建一个临时表。
CREATE TEMPORARY TABLE IF NOT EXISTS temp_compiled_properties AS
(SELECT
item_id,
MAX(IF(property_name = 'color', value, NULL)) AS color,
MAX(IF(property_name = 'size', value, NULL)) AS size,
...
...
...
FROM
properties
GROUP BY
item_id);
2) 创建模型
//this is not a typical ORM model and cannot be accessed as such
//You must first run the init function before data can be accesses
//You can read data from the model, other methods may work, but not all ORM functions have been tested with this model.
class Model_Properties_Pivot extends \Orm\Model
{
//--------------------------------------
//Table Details
//--------------------------------------
protected static $_table_name = 'temp_compiled_properties';
protected static $_primary_key = array('item_id');
//dont define properties, ORM will automatically query the temp table to get these
//protected static $_properties = array();
public static function init($game_id)
{
$sql = "CREATE TEMPORARY TABLE IF NOT EXISTS temp_compiled_properties AS
(SELECT
item_id,
MAX(IF(property_name = 'color', value, NULL)) AS color,
MAX(IF(property_name = 'size', value, NULL)) AS size,
...
...
...
FROM
properties
GROUP BY
item_id)";
DB::query($sql)->execute();
}
3)读取Model中的数据
Model_Properties_Pivot::init(14);
$all_data = Model_Properties_Pivot::query()->get();
另外,我研究了 EAV 并让它工作。问题是我的“属性”字段实际上是另一个外键(自动增量编号)。这意味着我无法使用它,因为它是一个自动增量编号。基本上,我不能像这样访问它$properties->10