简体   繁体   中英

Phalcon Model Relationship Select

I have two models as such:

Inv (invoice)

InvItem (invoice line items)

When attempting to retrieve the InvItem through the Inv model, I get a 500 error. No error is reported on screen or in the log file. Reference https://docs.phalconphp.com/en/latest/reference/models.html#unidirectional-relationships

Service Code

$invoice = Inv::findFirst($id);
$item = $invoice->invItem;

Inv Model Relationship

Primary: inv_id
$this->hasMany('inv_id', 'InvItem', 'inv_id');

InvItem Model Relationship

Primary: inv_item_id
$this->belongsTo('inv_id', 'Inv', 'inv_id');

Update (Added Models)

InvItem Model

class InvItem extends InvBaseModel
{

    /**
     *
     * @var integer
     */
    public $inv_item_id;

    /**
     *
     * @var integer
     */
    public $inv_id;

    /**
     *
     * @var string
     */
    public $item;

    /**
     *
     * @var string
     */
    public $desc;

    /**
     *
     * @var integer
     */
    public $quantity;

    /**
     *
     * @var double
     */
    public $rate;

    /**
     *
     * @var double
     */
    public $disc;

    /**
     * @var double
     */
    public $total;

    public function initialize()
    {
        $this->belongsTo('inv_id', 'Inv', 'inv_id');
    }
}

Inv Model

class Inv extends InvBaseModel
{
    /**
     *
     * @var integer
     */
    public $inv_id;

    /**
     *
     * @var integer
     */
    public $app_id;

    /**
     *
     * @var integer
     */
    public $acct_contact_id;

    /**
     *
     * @var integer
     */
    public $status;

    /**
     *
     * @var string
     */
    public $create_date;

    /**
     *
     * @var string
     */
    public $due_date;

    /**
     *
     * @var string
     */
    public $inv_date;

    /**
     *
     * @var string
     */
    public $sent_date;

    /**
     *
     * @var string
     */
    public $paid_date;


    public function initialize()
    {
        $this->hasMany('inv_id', 'InvItem', 'inv_id');
    }
}

I solved this problem by using the namespace in the relationship model.

public function initialize()
{
    $this->hasMany('inv_id', 'Multiple\Invoice\InvItem', 'inv_id');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM