Yii Framework Forum: Change the search function based on related table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Change the search function based on related table Rate Topic: -----

#1 User is offline   francesco89 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 17-March 15

Posted 14 March 2018 - 09:23 AM

Consider these three tables with their relations:
Maker
'analyzedObjects' => array(self::HAS_MANY, 'AnalyzedObject', 'maker_id'),

AnalyzedObject
'imagings' => array(self::HAS_MANY, 'Imaging', 'object_id'),
'maker' => array(self::BELONGS_TO, 'Maker', 'maker_id'),


Imaging
'object' => array(self::BELONGS_TO, 'AnalyzedObject', 'object_id'),

Now, in the search form related to Imaging I would like to let the user search an imaging based on the maker. How should I modify the search() function in Imaging.php and the form in _search.php?

Thank you!
0

#2 User is offline   Silvio Porcellana 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 31-January 18

Posted 14 March 2018 - 01:12 PM

Can't you use
with
in your search depending on the field you are searching for? Basically joining the tables as you need them.

More info: http://www.yiiframew...y-eager-loading
0

#3 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,641
  • Joined: 16-February 11
  • Location:Japan

Posted 15 March 2018 - 06:55 AM

/* Moved from "2.0" to "1.1" */

Hi,

The following wiki article has been a "MUST READ" for Yii 1.1 development.

Searching and sorting by related model in CGridView (http://www.yiiframew...el-in-cgridview)

You can not apply the wiki article just as is to your use case, but it surely will be a help.

1) You have to declare a public property "maker_search" in your Imaging model.

2) In the search() method of Imaging, you have to do it like the following:
$criteria = new CDbCriteria;
$criteria->with = array( 'object', 'object.maker' );
...
$criteria->compare( 'maker.name', $this->maker_search, true );

1

#4 User is offline   francesco89 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 17-March 15

Posted 22 March 2018 - 10:12 AM

Thanks. The visualization and sorting work. Can you suggest me how to edit the advanced search form? This is where I'm starting from:

...
<div class="row">
		<?php echo $form->label($model,'maker_search'); ?>
		<?php echo $form->dropDownList($model,'maker_search',$this->getMakers(), array('prompt' => '--Select a maker--')); ?>
	</div>
...

0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,641
  • Joined: 16-February 11
  • Location:Japan

Posted 23 March 2018 - 08:32 PM

Hi francesco,

Have you already implemented "getMakers()" method? And what does the item array (the return value of the method) look like?

I was thinking about a simple text input for "maker_search", because it is supposed to hold an arbitrary string for maker names.

If you want to use a dropdown for searching by the maker, you may consider using 'maker_id' instead of 'maker_search' in order to handle the id of the maker.

In "search()" method:
$criteria = new CDbCriteria;
$criteria->with = array( 'object', 'object.maker' );
...
$criteria->compare( 'maker.id', $this->maker_id );


In the view:
<?php
$options = CHtml::listData(Maker::model()->findAll(), 'id', 'name');
echo $form->dropDownList($model, 'maker_id', $options, array('prompt' => '--Select a maker--')); 
?>


Note that CHtml::listData() is a convenient method that you can use to construct the options array for a dropdownlist. Check it in the API.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users