PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 30, 2021

[FIXED] how to get all related model IDs in Yii?

 December 30, 2021     php, yii     No comments   

Issue

Suppose $model hase some items (one to many relationship), So in Yii $model->items returns an array of item models.
How can I get an array of IDs of related items. This means each element of returned array is an integer.


Solution

You should simply write your own function for this, e.g.

public function getItemsIDs()
{
  $ids = array();
  foreach($this->items as $item)
    $ids[] = $item->id;
  return $ids;
}

After you just have to call $model->itemsIDs.

EDIT : as darkheir said in its comment, you should consider using DAO.



Answered By - soju
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing