I've got a small question about Creating Aro-s in Cakephp
http://book.cakephp.org/#!/view/1547/Acts-As-a-Requester
I'm using code provided in cake-s tutorial (see the link), the problem is that alias for the aro-s are not set. how can this be achieved?
For the purpose of going through the tutorial quickly, you can simply set the alias using your favorite database managing program (phpMyAdmin or the like).
Edit1:
Go for the afterSave callback in the model under concern and set the alias from there.
Add something like this in your User Model.
public function afterSave() {
$this->Aro->save(array('alias'=>$this->data[$this->alias]['name']));
}
Change 'name' by the fieldName you want to use as alias for the Aro. Same in the Group Model.
Saludos.
Related
My question is if it is possible to add all the fields directly to a new model via Eloquent.
I guess it would be something like
php artisan make:model MyModel --fields=?
However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated.
Thanks in advance
If you mean table's column by fields then:
Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides, model automatically work with your database table's columns as its property.
So, now you may want to define columns while creating migration, not while creating model. There is library to serve this demand named as Generator(https://github.com/laracasts/Laravel-5-Generators-Extended) maintained by Laracasts.
Using this generator you can generate migration file to create table in DB specifying their column names and their type also. Here is a example from their Github repo, how you can do this:
php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"
You can checkout their documentation for more information. Best of luck.
It's not possible with make:model or make:migrations commands, but you can create your own console command and add this feature by yourself.
Also, take a look at source code of make:model and make:migration commands to get some ideas on how to do that.
it looks like only built in options are --migration and -m to include a migration with the model generation. L5.3 Docs
There does look like there is a package for L5.0, which looks like it would work in 5.*+. It is put out by Laracasts:
https://github.com/laracasts/Laravel-5-Generators-Extended
It also looks like you can make a custom solution as well:
https://laracasts.com/discuss/channels/tips/l5-artisan-command-makemodel
Hope that helps!
No options while creating a model,
This is my terminal output (laravel 5.3) while i check,
You don't need to mention fields while creating model.
Ex:- based on the rules you should keep the names as like below,
model name as User
table name as users
then the model automatically handle everything, you don't need to mention the table/fields name.
I was looking for the same thing myself, as I used to work like that in previous frameworks, but could not find it, or at least not as I wanted it, so I did my thing. You can check it out if you like:
https://github.com/Triun/laravel-model-base
It will read your database, and create the laravel eloquent models for you.
It is meant to be very flexible, so the configuration may be a little complex, and I guess that I didnt' catch up with the documentation, but you can ask me if you don't know how to make it do what you want.
Basically it has 4 customization levels:
By out of the box modificators, configurable by the config files.
Including interfaces and traits to your auto-generated models.
Create your own modificators. Classes where you receive the model skeleton before it is saved, so you can add or remove properties, methods, etc.
Generate the model base, but edit yourself the final model.
And, of course, suggestions and contributions are more than welcome.
I am new to CakePHP.
I want to have a 'settings' page that links to other models. i.e. "groups" and "accounts".
I created a model/and controller for "Settings" as that is what I figured I had to do. Is this a correct assumption?
Now in the 'settings' page, I want to display the MySQL count of "groups" (want to do other things also, but quickly realizing I'm missing something).
in other words, I want to echo the count of [Group Model] in the database in the [Settings Model]
When I try to do that though, CakePHP freaks out and says that there is no table for "Settings" ... and I don't want there to be one.
Whenever I have issues 95% of times you guys show up in google w/answer, but had trouble finding/and wording this one.
Many thanks for your help!
It makes no sense to have a Settings model if you don't have a DDBB table or other datasource associated with it. Plus, model names are by convention singular, so it should be Setting.
You made the SettingsController and a view to show some mixed info about your app, which is ok, but you don't need the Setting model. What you need to do is tell Cake you'll use the Group model in this controller, by doing
public $uses = array('Group');
In most basic tutorials you might not see this since Cake automagically wires up related controllers and models, like GroupsController and Group model.
Now that your SettingsController has the Group model available, you can do:
$this->Group->find('count');
to find what you need.
You'll probably need to reference more models in SettingsController, like Account.
I sometimes see the function parentNode() in cakePHP models. What is the purpose of that function and when should I use it? If I don't use it, what problems am I going to encounter.
On this website, they say that you need it so that your model can have ACL behaviors, but what if I just run the build_acl command after creating my Model, isn't that the same thing? Can someone shed some light on this please?
Thank you
build_acl() is useful as a one-time execution to populate your acl tables with the current controllers+actions. This is useful as a base to get you started.
parentNode() is called by the model behaviour in afterSave() to maintain the hiearchy during the life of your application. So when you manually (or dynamically) add AROs / ACOs later on (e.g. 5 months from now) everything will still work. Especially true if you add ACOs with custom aliases.
BTW, best ACL tutorial I've found:
http://net.tutsplus.com/tutorials/php/how-to-use-cakephps-access-control-lists/
I have a model with TranslateBehavior attached to it and I wonder if there is a method to remove only one translation of specific item and use only standard model methods (=not provide SQL query on i18n table explicitly, because I want to be absolutely independent of DB engine) and not set it blank but really remove.
Simple situation example:
There is an article in DB with translations in English and German. One day editor decides not to provide this article in German and wants to remove it from DB (but English version should be still available). And now appears the question I wrote above.
Thank you in advance for your help.
As for now after examining afterSave callback in the behavior class I see only some kind of workaround - to use the core model I18nModel defined in cake/libs/model/behavior/translate.php (bottom of the file) and use delete method with manually set all conditions that normally are set by translateBehavior.
The I18nModel model can be used in standard way:
var $uses = array('SomeModelUsingTranlateBehaviour','I18nModel');
i was just looking for a bit of advice, currently my DB design is that one user has many blog postings. Now i have a user class and a blog class.
However im not sure about the proper way to get all blog posts for that user. Do i create a getAllBlogs() function in the user class which returns blog objects or do i create a main blog class that you can search by user, so it'd be getAllBlogsForUser($id)
Thanks :-)
I'd personally use the later option. This is because the blog class knows how to deal with the blog table(s). I wouldn't want to write blog-specific DB code in the user class. On the other hand, you still can add User::getAllBlogs() as a wrapper around Blog::getAllBlogsForUser().
It's really up to you. Since users are pretty tightly coupled to blogs, you could do both.
Consider how nicely they could play together:
<?PHP
class User {
protected $_blogs;
public function getBlogs($force=false){
if (empty($this->_blogs) || $force){
$this->_blogs = BlogClass::getBlogsByUser($this->user_id);
}
return $this->_blogs;
}
}
Now you can grab a user's blogs whenever you want, and it will fetch them only when necessary. The $force parameter can be used to force a reload.
i am not sure if this is actually a helpful answer .. however if your db design is done correctly with the correct normalization practices in place .. it should be indicative of your class layouts as well. as User is an independent entity, and posts use user_id or something as the foreign key , it should clearly give you the idea who should be the master. Users can exist with-out posts , however posts can not exist with out users. So it makes sense to put the get posts method in the postings class rather than the users class.
I'd use both, like User::getPosts and Posts::findByUserId ("Posts" as model name sounds better to me than "Blogs" ). Both methods have similar functionality and which one to use depends on the context, for example, frontend will rather use Users model, while in admin interface 'finder' methods could be more appropriate.
I would create both. Your User::getBlogs() could use Blogs::getBlogsByUserId($idUser) since when you are in the user context, you will have access to the user's id to pass to the Blogs method.