I developed small CakePHP application, and now I want to add one more table (in fact, model/controller/view) into system, named notes. I had already created a table of course.
But when I run command cake bake model, I do not get table Notes on the list. I can add it manually, but after that I get some errors when running cake bake controller and cake bake view.
Can you give me some clue why I have those problems, and how to add that new model?
I would also check your app/config/database.php to ensure that you are using the correct database configuration. You may well have added the table to a different database perhaps and the bake is picking up the other database. Also, and this may be obvious, but check you are in the right project, it's easy to be in a different folder and not realise, especially if you have lots of projects.
I'm not aware of a limit on the bake listing. I would check your database to make sure the table exists and has some columns. You can always open up the console bake script and check for a limit and increase it if needs be.
I found solution!
I had to delete all from cache directory, /app/tmp/cache/models
Now it works!
:-)
When you say added it manually, do you mean added the note.php model? If not, you may want to try that. Verify that the model name is correct for the following:
file name: note.php
class name: class Note extends AppModel
table name: notes
Also, be sure the notes table has the id column and it is set to primary key.
If this does not push you in the right direction, please post your notes table schema here. Also, have you had success in baking other things in your app? Have you upgraded anything?
Please change the following farameters to bake:
For Controller:
/cake/console/libs/tasks/controller.php
function listAll($useDbConfig = 'default') {
change to :
function listAll($useDbConfig = 'YOUR DB CONFIG NAME') {
NOW DO CAKE BAKE for CONTROLLER! ENJOY!
Related
I am using Cakedc Users plugin and I want to add a small entry to the Users table which is "Balance" (integer)
I read the extending part in the documentation and I honestly got dizzy from all the modifications that I have to do
I don't want to rewrite the whole thing just for a small entry, Is there anyway I can add it to the table with minumum modification or rename another entry that I don't need, like "tos_date" or something
Well, let me try to help about how extending the model should be:
Modify the users table (via migrations or manually) and add the columns you want.
Copy the template files with the forms, from the plugin itself to your app under the folder src/Template/Plugin/CakeDC/Users/name_of_the_controller/name_of_the_action, then modify the forms to add a new control for your custom column
You're done
Thanks!
As mentioned in the title, I am encountering an issue concerning the convention between models and database. I created a new model named "Media". In my database, I created a new table named "medias".
Following the documentation, Laravel is assuming the table name should be the plural of the model name. In my case, it is actually not working, I need to set manually the property table in order to get the desired behavior.
Laravel version : 5.3
Mamp | php 7.0.13
I will really appreciate your help, thanks.
Media is an exception. Laravel is smart enough to know that there is no plural for media. So you should create media table instead.
You can see all exceptions here:
Words that should not be inflected.
private static $uninflected = array(
....
'?*media',
....
);
Media is the plural form of Medium.
So either you can create a table with media name or in your Media model you can write this,
protected $table = 'medias';
I am trying to add some extra fields in the Cakedc Users plugin default Users table
But I can't figure it out how to do it, I didn't find anything in the documentation about this problem, I found a similar question here
But that person asked for a lot so he didn't get a lot of help, I also tried adding the extra field in the Mysql users table and in the register.ctp template , But I find it's value is empty
the question you mention is related to the previous version of the Plugin (for CakePHP 2).
You are doing right now, but the problem is the User Entity being too strict and blocking mass-assignment https://github.com/CakeDC/users/blob/3.1.5/src/Model/Entity/User.php#L30 (which is possibly a good thing to change in the Plugin itself to allow an even easier override). I'll add a ticket for this in a bit :)
In the current version it's very easy to extend the users table and add your own columns.
For example, let's say you want to add a new column to your users table "phone".
Add a new column in users table (usually involving a migration, you can "bake" this migration using
bin/cake bake migration AddPhoneToUsers phone:index
Run the migration to apply the changes
bin/cake bake migration migrate
Now follow instructions here https://github.com/CakeDC/users/blob/master/Docs/Documentation/Extending-the-Plugin.md#extending-the-model-tableentity to:
Create empty Model and Entity classes extending the plugin classes
Override $accessible property in your new Entity to something like
protected $_accessible = [
'*' => true,
'id' => false,
'role' => false,
];
Lastly, add this override to your bootstrap.php file after loading the Plugin
Configure::write('Users.table', 'MyUsers');
The plugin will pick your customized Table and use the new fields coming from your custom register.ctp page.
We've created an improvement ticket here > https://github.com/CakeDC/users/issues/311 to relax $_accessible fields.
Thank you,
I continue building my CakePHP application. Now I've created all the database schema in MySQL, run some "cake bake all" and I have lots of models, views and controllers, and I'm gonna personalize them.
In many of my models I have this fields:
company_id
created_by
modified_by
As you can understand, the first field is the id of the owner of the "row", created_by is the id of who created the row and modified_by the latest person who updated it.
I know I can create a beforeSave filter in the model and update all the data (I suppose that I can know if I'm creating or updating a row, isn't it?), but just now I have 15 different models and I hope the app will grow more, so it's a lot of repetitive code to write. And, it breaks the DRY principle.
And my question is: Is there any "elegant" way to solve this problem? Maybe creating a class extending AppModel with a beforeSave filter for updating the fields, and that all my models inherit from the new Model class instead of AppModel?
Thanks for your help!
Make it a behaviour and load it for models that need that functionality.
See http://book.cakephp.org/2.0/en/models/behaviors.html#creating-behaviors
I think the most appropriate way is to create Behaviors.
You can set up the beforeSave callback in the behavior like what you have in your model.
Here is the link to the documentation http://book.cakephp.org/2.0/en/models/behaviors.html#behavior-callbacks
You can also check as example dereuromark's WhoDidItBehavior.
I am pretty new to Django and just got a job that involves maintaining and adding features to a site I did not design, so I am still kind of confused about the structure and what not of the project. The site is using South for database migrations and I've got a hang of using it to add new applications to the project. The trouble I am having now is that I need to delete a certain field in a model because it is no longer needed and on the admin page it is required to be filled out. From my understanding of Django so far it appears to be a custom field. It is defined like this in its own separate library application(still not sure if thats the right lingo).
class Genre(models.Model):
name = models.CharField(max_length=255)
def __unicode__(self):
return u"%s" % self.name
Here is the models that uses the custom field if that helps out any.
class Entry(models.Model):
artist = d51fields.ForeignKey(Artist, instantiate_fn=instant_artist)
album = d51fields.ForeignKey(Album, js_methods=['match_artist_and_startswith'], instantiate_fn=instant_album)
track = d51fields.ForeignKey(Track, js_methods=['match_album_and_startswith'], instantiate_fn=instant_track)
genre = models.ForeignKey(Genre)
submitted = models.DateTimeField(auto_now_add=True)
is_rotation = models.BooleanField()
dj = models.ForeignKey(DJ)
show = models.ForeignKey(Show, null=True, blank=True)
objects = EntryManager()
def __unicode__(self):
return "%s [%s]" % (self.artist, self.track)
class Meta:
verbose_name = "entry"
verbose_name_plural = "entries"
I've looked at the documentation for migrating custom fields but it is all really confusing for me, so I am looking for some more help. I just want to get rid of the table holding the Genre field and clean up the dependencies with the foreign keys associated with it. Do you think I should write some custom rules for South and use a migration or just try and do it manually in Postgresql. I tried doing it with just Postgres and I failed miserably.
Any guidance would be greatly appreciated. If you want more info about the situation just ask and I can add it to the post. I have a feeling there is a lot of dependencies I will have to deal with, but hopefully there is a simple fix.
Also if some one knows how to get a good view of the database structure that would be great.
Thanks so much. All of you guys are great.
Edit1
Here what I got when I removed the ForeignKeys and then ran
manage.py schemamigration logs --auto
! Cannot freeze field 'logs.entry.artist'
! (this field has class d51_admin_autofk.fields.ForeignKey)
! Cannot freeze field 'logs.entry.album'
! (this field has class d51_admin_autofk.fields.ForeignKey)
! Cannot freeze field 'logs.entry.track'
! (this field has class d51_admin_autofk.fields.ForeignKey)
! South cannot introspect some fields; this is probably because they are custom
! fields. If they worked in 0.6 or below, this is because we have removed the
! models parser (it often broke things).
I am not totally sure what sort of action I should take next. I looked into the South documentation and it wasn't too clear about how to write the rules for migrating things like this.
I don't see any custom field anywhere in the code you posted. All I see is two models, all containing standard fields shipped with Django.
If I understand correctly, you can just delete all ForeignKey references to your Genre model. Then run ./manage.py schemamigration <yourappname> --auto. This will ask you for a default value for the genre field in the Entry model, provide an ID of some kind. (This is because migrations can be applied both forwards and backwards, so if you try to undo the migration, this is the value that will get inserted in all your model instances.)
Finally, just applying the migration should make it happen: ./manage.py migrate <yourappname>.
After that you should be safe to drop the table storing your Genre model.
Be sure to try this on a development server though, just to make sure it doesn't blow up. (-;