Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where to put model rules()? #9

Closed
mootensai opened this issue Jul 27, 2015 · 13 comments
Closed

Where to put model rules()? #9

mootensai opened this issue Jul 27, 2015 · 13 comments
Assignees
Labels

Comments

@mootensai
Copy link
Owner

Where to put model rules()?
Should i put it on base model or extended model?
Do you like to change your generated rules?
I think i'll put it on the extended model?
What do you guys think?

@arjenmeijer
Copy link

I do change the default rules very often. So I agree it should be in the extended model.

@realmarkcross
Copy link

In the extended model seems to make sense, but why not an option?

@chitwarnold
Copy link

Extended Model is the best place

@mootensai
Copy link
Owner Author

After I think about it, what if it's best that i generate on both model and the extended model merge parent rules with extended rules.. so if DB structure is changed, you don't have to type manually the validation of the new column..

what do you guys think?

@arjenmeijer
Copy link

For me, the difference between model and extended model is complicating the code. Every time I have to ask myself where to put and find things. Give an option to generate just one model with all code in it. Gone are the questions!

@mootensai
Copy link
Owner Author

you should not type anything on the base model actually.. base models are for generated by gii only. so u don't have to move any code if you want to re-structure your tables.

on yii 1(i built the related CRUD generator too), i experienced that if i want to restructure the table, I have to backup my functions on the models before i re-generated the models

@arjenmeijer
Copy link

Well, sometime I have add new complex relations. If I put them in the extended model, I have two places with relations. Mmmm...

@mootensai
Copy link
Owner Author

                                                                                  May I see your complex code as an example?                                                                                                                                                                                                                                                                                                                                        Sent from my BlackBerry 10 smartphone on the 3 Indonesia network.                                                                                                                                                                                                                From: Arjen MeijerSent: Senin, 15 Februari 2016 15.56To: mootensai/yii2-enhanced-giiReply To: mootensai/yii2-enhanced-giiCc: Yohanes CandrajayaSubject: Re: [yii2-enhanced-gii] Where to put model rules()? (#9)Well, sometime I have add new complex relations. If I put them in the extended model, I have two places with relations. Mmmm...

—Reply to this email directly or view it on GitHub.

@mootensai mootensai self-assigned this Feb 15, 2016
@arjenmeijer
Copy link

Not complex, but nevertheless. A function to return the name of an id with the id attached. The same function I use everywhere for the select2 pulldown.

/**
 * @return array with two fields
 */
public function getIdnameselection()
    {
        $spelvormen = Spelvorm::find()
                    ->orderBy('spelvormname')
                    ->all();
        return ArrayHelper::map($spelvormen, 'spelvorm_id',
            function($model, $defaultValue) {return $model['spelvormname'].' (id: '.$model['spelvorm_id'].')';});
    }

@mootensai
Copy link
Owner Author

Well, basically u dont type anything on the base models.. so that function
better be put on extended models..

On Mon, Feb 15, 2016, 16:15 Arjen Meijer notifications@github.com wrote:

Not complex, but nevertheless. A function to return the name of an id with
the id attached. The same function I use everywhere for the select2
pulldown.

/**

  • @return array with two fields
    */
    public function getIdnameselection()
    {
    $spelvormen = Spelvorm::find()
    ->orderBy('spelvormname')
    ->all();
    return ArrayHelper::map($spelvormen, 'spelvorm_id',
    function($model, $defaultValue) {return $model['spelvormname'].' (id: '.$model['spelvorm_id'].')';});
    }


Reply to this email directly or view it on GitHub
#9 (comment)
.

@arjenmeijer
Copy link

Yes, but than I have two places to store getFunctions. What (logically) differentiates the gii generated ones from the hand coded ones? I my head it are just getFunctions which should be in one place.

@mootensai
Copy link
Owner Author

so it seems that your functions is for magic getter method. I always put that code on the extended model.

Logically it's the same, but be warned that your code will be rewritten if you change your DB structure & wants to re-generate the model.

you better move your getFunctions to extended models (manually) if you want to be in one place. (which will override your base model if you re-generate it)

@arjenmeijer
Copy link

Thanks for the explanation. That is what I am missing most in Yii2. I do not know WHY something is chosen, only that is chosen. Therefore, I can not re-think the decisions and make a correct extension of the thoughts.

mootensai added a commit that referenced this issue Feb 20, 2016
mootensai added a commit that referenced this issue Feb 20, 2016
search form & gridview improved, fixes #9 
Scrutinizer Auto-Fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants