簡體   English   中英

Phalcon查找與模型關系的條件

[英]Phalcon find with conditions of models relation

我有這個模型代碼:

class TvguideChannel extends Model{

    public function initialize() {
        $this->setSource('tvguide_channel');
        $this->setConnectionService('db');

        $this->hasMany('code', __NAMESPACE__.'\Tvguide', "ch_code", ['alias' => 'Tvguide']);
    }

    public function getSource() {
        return 'tvguide_channel';
    } 
}

和控制器:

    $data = TvguideChannel::find();
    $dateDetails = $data->getTvguide(['order' => '$_POST["time"] DESC']);

    $paginator = new PaginatorModel(
        array(
            "data" => $data,
            "limit" => $limit,
            "page" => $curPage
        )
    );

    $items = $paginator->getPaginate();

這是行不通的。 如何在控制器中使用Tvguide模型中的列? 感謝幫助。

您的控制器代碼將返回TvguideChannel實例。 要訪問該關系,您必須使用上面定義的別名。

$data->Tvguide

上面應包含該記錄的所有Tvguide。

這是hasMany()的更詳細的示例:

$this->hasMany(
    'id', // The primary key in your main table
    'Models\ServicesVideos', // The Model you are joining
    'service_id', // Foreign key for main table
    [
        'alias' => 'videos', // Alias which you use later to access the relation
        // additional parameters like where, order, limit etc...
        'params' => [
            'order' => 'position ASC',
            'conditions' => 'type = :type:',
            'bind' => [
                'type' => get_class($this)
            ]
        ]
    ]
);

UPDATE :將參數傳遞給關系本身的示例。

型號代碼:

class Orders extends BaseModel
{
    public function initialize()
    {
        $this->hasMany('id', 'Models\OrderDetails', 'order_id', [
            'alias' => 'details',
        ]);
    }
 }

控制器代碼:

// We want only related records with price higher than 9.00
$order = \Models\Orders::findFirst(17);
$orderDetails = $order->getDetails([
    'conditions' => 'price > 9.00'
]);

更新2

find()方法返回一個Resulset,這意味着您不能直接使用$data->getTvguide 您必須遍歷每條記錄並訪問其相關條目。

$data = TvguideChannel::find();
foreach ($data as $item) {
    // This will return related guides for each channel.
    $dateDetails = $item->getTvguide(...);
}

如果您需要從相關模型中查找位置,則需要使用查詢生成器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM