yiisoft / yiisoft/active-record

Idea: avoid double declare link and type of relation if used via()

Open
#57 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
119
Forks
38
Avg merge
1h 11m
Merged PRs (30d)
1

Description

#14238
What about AR function?
yii\db\BaseActiveRecord

    /**
     * Get a relation from the another class relation definition
     * @param ActiveRecord|array|callable|string $target the model class who have relation
     * @param string $relationName relation name in target (case sensitive, without 'get')
     * @return ActiveQuery
     */
    public function hasRelation($target, $relationName){
        $target instanceof ActiveRecord || $target = Yii::createObject($target);
        /** @var ActiveQuery $relationQuery */
        $relationQuery = $target->getRelation($relationName);
        $relationQuery->primaryModel = $this;
        return $relationQuery;
    }
class Order extends ActiveRecord
{
    public function getItems(){
        return $this->hasRelation(OrderItem::className(), 'item')->via('orderItems');
    }
    public function getOrderItems()
    {
        return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
    }
}

It's avoid double declare link and type of relation (multiple)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading yii\db\BaseActiveRecord and the relation path involving hasRelation() and via(). Compare the Order and OrderItem example to the current relation declarations, then define how duplicate link and relation-type declarations should be avoided before identifying suitable tests or acceptance criteria.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.