Giix — Gii Extended

Hi mentel,thanks for your awesome extension!

I have set up a Category-Product MANY_MANY relation,and I am trying to call a CActiveDataProvider for the Product model filtered by a specific Category to which it belongs.(to use it in a ListView).There is no foreign key connecting the two models,only the pivot table,so I don’t know what to use in CDbCriteria for the CActiveDataProvider argument.Thanks in advance !

Hello everybody.

Things are going quite well with this extension however I have run into a minor issue that perhaps one of you smarter people might be able to help with.

Example:

TABLES


Tourist: ID, NAME

Place: ID, NAME

rel_tourist_visited: Tourist_ID, Place_ID

rel_tourist_wishlist: Tourist_ID, Place_ID

Pretty simple, in the example a TOURIST may have visited many PLACES and a PLACE may have had many TOURISTS who have visited.

And, a TOURIST may wish to visit many PLACES and the PLACE may have many TOURISTS who wish to visit.

Problem:

If there is only one many_many between TOURIST and PLACE things are fine, it works exceptionally however as I have two I have run into issues.

When you choose to add/edit a record it will show a many_many list of checkboxes, but only for ONE of the relationships (I think the second in the list, alphabetically).

Probably simple, but beyond me, any ideas? :)

Hello,

When you have a many_many relation and you want to update with saveWithRelated, if update have to delete a line in your relation table, it can cause some issues :

$pivotModel = GxActiveRecord::model($pivotClassName)->findByPk($value);

=> wait for an int but with 2 FK in the relation table it gaves me an array :

A solution is to replace in line 481 of GxActiveRecord.php :


// Delete one active record at a time.

foreach ($deleteMap as $value) {

	$pivotModel = GxActiveRecord::model($pivotClassName)->findByPk($value);					

	if (!$pivotModel->delete()) {

		return false;

	}

}

By :


// Delete one active record at a time.

foreach ($deleteMap as $value) {

  if(is_array($value)){

    $pivotModel = GxActiveRecord::model($pivotClassName)->findByAttributes($value);				

  }else{

    $pivotModel = GxActiveRecord::model($pivotClassName)->findByPk($value);				

  }

  if (!$pivotModel->delete()) {

    return false;

  }

}

(Notice that in your controller you will have to set batch to false):


if ($model->saveWithRelated($relatedData, true, null, array('batch' => false))) {

  $this->redirect(array('view', 'id' => $model->id));

}

Thanks you for this nice extension :)

SOLVED





    $prod_criteria=new CDbCriteria;

        $prod_criteria->with=array('categories'=>array(

                                                                                                          'on'=>'categories.id=:cat_id' ,

   'together'=>true,

      'joinType'=>'INNER JOIN',

                                                                                                         	'params'=>array(':cat_id'=>$_GET['cat_id'])

                                                                                                            ));


        $prod_dataProvider=new CActiveDataProvider('Product', array(

                                                                                                                                    'criteria'=> $prod_criteria,

                                                                                                                                    'pagination'=>array(

                                                                                                                                                                         	'pageSize'=>2,

                                                                                                                                                                           	),


                                                                                                                                                  ));



Maybe this will help you…(sorry for my english)

My case TABLES:

person

organization

person_workfor_organization (relation)

person_memberof_organization (relation)

I can suggest that problem is about relation name generated in generateRelations() in ModelCode.php line 272 called

by "$this->relations = $this->generateRelations();" in GiixModelCode.php line 67 :

Inside generateRelations() is defined $relationName=$this->generateRelationName($table0, $table1, true); in ModelCode.php line 296

Well, creating “person” model with giix $relationName value for “person_workfor_organization” and “person_memberof_organization” tables was “organizations” so i’ve “lost” a relation caused by same array key assignment:

$relations[$className0][$relationName]=“array(self::MANY_MANY, ‘$className1’, ‘$unprefixedTableName($pks[0], $pks[1])’)”;

$relations[‘person’][‘organizations’] = “array(self::MANY_MANY, ‘Organization’, ‘person_memberof_organization(id_person, id_organization_memberof)’)”

$relations[‘person’][‘organizations’] = “array(self::MANY_MANY, ‘Organization’, ‘person_worksfor_organization(id_person, id_organization_worksfor)’)”

[b][size="2"]My solution? [/size]

simply replace "$relationName=$tableName;" instead of "$relationName=$this->generateRelationName($table[X], $table[X], true);" in ModelCode.php line 29x [/b]

Hi, nice work !!

exactly what I need !

but …

is there any way for this:

I have a table with fk , and i want to use the description of the related table eith this:

representingColumn

public static function representingColumn() {


	return 'fk_grado_id'; //Ver esto !! tira la consulta SQL a la Base


}

like :

return ‘fk_grado_id->descripcion’.

I try but I get an SQL error !!

Just tell me if you hava any better idea.

Best Regards

I understand that a pivot table should only be used for maintaining a m2m relationship between two tables and there should not be any extra fields other than the the FK columns that compose the PK.

But sometimes, we really want to add some extra fields in the pivot table. Since it contains extra data, this makes it not only serve the purpose of a pivot table but also becomes a model itself. Then we may probably prefer to add an extra id column to use it as the PK instead. Can or will giix generate proper codes to handle this case in the future?

Currently, giix generates checkboxes in the view for a m2m relationship but if there is an extra column, for example a varchar(100) column, in the pivot table, giix can simply generate a text field beside the checkbox.

By the way, I really like your extension, it’s very useful. B)

Hi! I can’t get this to work, which is awkward because it is only 3 steps and even I should be able to do that.

When I try use giix crud generator I get include(GxActiveRecord.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

I’ve tried having the files in both

protected/extentions/giix-crud

protected/extentions/

When trying to create a Model I get : Class ‘GxActiveRecord’ does not exist or has syntax error.

To me it seems like I have some kind of path issue… But since I’ve tried all likely,unlikely places to put the files I have to ask here.

The settings in my main config file under import and gii is also correct.

When I read the readme under requirements it’s not 100% clear what you mean so just be sure:

On my localhost I use wamp with latest mysql and apache.

I meant that it should work with your latest Apache and MySQL. But WAMP is not officially supported because I can’t test it.

Check your PATH after including giix and after including some other file.

good

hi, I added some lines of code to GxActiveRecord.

the idea is to parametrize representing Column separators.

when using something like:




	public static function representingColumn() {

		return array('apellido','nombre');

	}



the view shows LASTNAME-FIRSTNAME

so I created a function: in GxActiveRecord




	public static function representingColumnSeparator() {

		return '-';

	}



and then in your model ( not BaseXxx, in the real one ):


	public static function representingColumnSeparator() {

		return ', ';

	}



looks like: LASTNAME, FIRTSNAME

Regards.

giix UNIQUE rule:

Hi, first of all, congrats for this excellent piece of code. I’m just starting with Yii and I found it very interesting.

Second, I found that giix does not create UNIQUE rule based on the UNIQUE flag of the field in the database. Is this an intended behaviour or is it a bug?

Again, thank you very much for this module!

Regards.

Hi jmariani,

Please try to generate with gii and check if it is ok.

giix uses gii to generate most of the code. If you don’t see the ‘unique’ rule, please open a ticket so they can check it.

Before opening a ticket, please double-check your schema. I don’t believe it’s a bug in Yii.

Hi! Thank you for your prompt response!

You’re right, Gii doesn’t create UNIQUE rule also.

Here’s my SQL (Produced by MySQL Workbench:




CREATE  TABLE IF NOT EXISTS `yiitest`.`MasterAccount` (

  `id` INT(11) NOT NULL AUTO_INCREMENT ,

  `rfc` VARCHAR(13) NOT NULL ,

  `name` TEXT NOT NULL ,

  PRIMARY KEY (`id`) ,

  UNIQUE INDEX `rfc_UNIQUE` (`rfc` ASC) )

ENGINE = InnoDB

AUTO_INCREMENT = 2

DEFAULT CHARACTER SET = utf8



Looks like Gii is not taking into account the UNIQUE INDEX directive.

jmariani, please read the Yii Conventions: database. Not following it may bite you later.

Also, it is important to read all the guide before working with Yii.

Thank you for your support.

Yii doesn’t generate rules for UNIQUE indexes. Maybe on 2.0, Qiang said.

Regards

I see that the admin view created by Gii has the advanced search form hidden by default, and Giix doesn’t. Is it by design or is it a bug?

Eitherway, the fix is adding <div class="search-form" style="display:none"> on the search form when created.

[SOLVED] [s]Also, I want to ask you if you can include a search form also in the index view or if you can give me or guide me to a way to add the search form into a index view.

[/s]

Adeus!

It is by design. But planned to be changed in a coming version.

Guys, how do I use GUID’s there with that extension? Everytime I use a table with int’s, it works.

For instance, i make the model and then the CRUD with a table(i.e User) with an ID type int, it works, i can go to my website/user and it works. If i use GUID instead of int into the field ID, it doesn’t work.

Any help about the issue would be appreciated!

Hi chumbx,

Welcome to the forum.

Like I said in the issue you reported,

Your issue seems to be related to a route configuration.

Please check these links:

http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#route

http://www.yiiframework.com/doc/api/1.1/CUrlManager

Also, don’t forget to read the Yii guide before you start working with Yii:

http://www.yiiframework.com/doc/guide/1.1/en/