S
S
Sergey Beloventsev2018-04-30 12:31:29
Yii
Sergey Beloventsev, 2018-04-30 12:31:29

How to use ArrayHelper with hasMany binding?

there is a connection

/**
     * @return \yii\db\ActiveQuery
     */
    public function getUserTacks()
    {
        return $this->hasMany(UserTack::class, ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTacks()
    {
        return $this->hasMany(Tack::class, ['id' => 'tack_id'])->viaTable('user_tack', ['user_id' => 'id']);
    }

The question is whether it is possible to get certain properties, for example the name of the Tack model, by running ArrayHelper ? or just foreach

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Beloventsev, 2018-04-30
@Sergalas

foundArrayHelper::getColumn($user->tacks,'name')

M
Maxim Timofeev, 2018-04-30
@webinar

ArrayHalper is made (hasMany) as and just for working with arrays of models, how did you use it with hasOne (one object) before?
In general, here is the guide: https://www.yiiframework.com/doc/guide/2.0/en/help...
And I advise you to get into the helper code, there is foreach at every step. So don't avoid it. Using a helper is a convenience and code reduction, but not critical if you don't use it somewhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question