Hello guys, I created a "promos" table and now, I am working on the CRUD functionalities of this module. The "create" functionality is done and I encountered no problems. My only problem is this when I am updating my model and it is very weird.
It seems that $this->model->where('id', $id)->first() cannot see and retrieve the list of columns. Here is the screenshot.
I already tried composer dump-autoload and php artisan clear-compiled hoping the problem will fix itself.
For additional reference, here is my schema, model and code:
Other notes: $this->model points to the Promo model
EDIT:
I did not display the controllers specifically the update method as stated by #OmarTarek. Our company is using RepositoryInterface Pattern. Instead of the normal View=>Controller=>Model when saving data to the database, our workflow is like this View=>Controller=>Repository=>Model
In my controller, my code is
While in my repository, my code is like this:
As you can see, I am inheriting the BaseRepository.php because it has all the necessary functions/methods for create, update and delete.
It is the BaseRepository
I highlighted the code that is giving the error.
EDIT II:
I already implemented the change suggested by #PaladiN. The error still displays and the update method still don't work.
You can remove the first() since the id is a primary key. Also you're calling the update statement again on updateData() method, you should remove that.
$this->model->where($key, $value)->update($data);
Also when you either define $fillable or $guarded, not both. When you define $guarded with an empty array, all the fields become fillable by default. Another thing would be to check if the model created in the constructor using make() is a valid model instance before proceeding.
sorry for the late update. I just fixed this error but I still don't know how this error happened. It seems that a mutator from our BaseModel.php interferes with the updating of data.
I tried overriding the mutator in my Promo.php model to make the update method work and the error no longer shows.
I still don't know what caused the error but I am eager to deliver this module first to our clients so I will investigate how this scenario happened next time.
Thanks guys.
Related
I received an update of a Phalcon model class and I had to update my local data table with the new attributes the model contains. Usually this kind of operation is not a problem, I simply launch an alter SQL query on the to add the columns, assign values to the object, call save and that's it.
However this time something strange happened: when I call save() on the model, everything is updated/created except the two new attributes I've added in my table. I checked the logs to take a look at the raw SQL query and the two new attributes are missing, the funny thing is that this operation worked the first times I tested it.
I think this error might come from the framework, after debugging my code I see clearly that the model takes the new values but for some reason cannot pass it to the SQL query.
Thus my question is the following: is there a way to force the Phalcon model to be sync again with my table?
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 need some help dealing with a relational table that is an entity due to the existence of an additional property.
Here is a gist of the entities in question: https://gist.github.com/chasepeeler/efd7efd890c58eafb81f
Do I have something configured wrong that is forcing me to do the flush in controller.php line 15?
I've also tried just updating the rank attribute of the queueItem record in the Queue::queueItems collection, but when I do that, it doesn't even save the changes to the database.
$queueItems->clear() does the same thing as clearQueueItems, but one time.
And if you want to override current queue state, you should just implement and call setQueueItems(ArrayCollection $queueItemList) method.
UnitOfWork will compute your changes to insert and remove new/deleted items.
Every OneToMany annotated field should implement setItems, addItem and removeItem methods, where Item is related entity name.
Your sortQueue method shouldn't persist and commit changes into database.
It should only return a sorted Collection.
Maybe I didn't get that, it's hard to say what you want to achieve, controller's code says me nothing.
It seems to me that having duplicate createTable() methods in both a migration and a model is a bad/dangerous thing. I would worry that someone will change the model and forget to change the corresponding migration (or vice versa), causing confusion and errors.
Does anyone know if it is possible (or wise) to simply call the createTable() method in the model from within the migration script?
If so, can someone point me to a working code sample so I can see how it is done?
I'm going to mark this as answered, per the comments above. It appears that the createTable() method does not belong in the model. I was fooled by a "more experienced" Laravel developer who did it that way.
:-/
Hy,
I've a short question about a weird error message I get on my Symfony 2 project.
I've an entity class User which retrieves its roles via Doctrine. This works perfectly! But...
I want let the User implement EquatableInterface, so I've added the User::isEqualTo method. This is where my error occurs. This line, especially $this->getRoles()is causing the error:
Symfony2: Call to a member function toArray() on a non-object
But the same toArray function usage inside User::getRoles() works great on the rest of the project. So I don't get what's wrong here.
Can somebody help me with it? Any help appreciated!
Update 1
Looking into the logs and using your current help, here are some insights:
$thisin getRoles always returns the entity user class, so nothing special there, but
After each isEqualTocall, $this->rolesreturns null, after that it doesn't.
Update 2
Here are my further insights:
I've added Konstantin's is_nullcheck, but it doesn't solve the actual problem.
As I could see in the logs during logging in, refreshUser is called and everything is perfect. Roles are found. After refreshUser isEqualTois fired and suddenly $this->rolesbecomes null and get_class($this->roles)returns user entity class (?!?!?) in comparison to Doctrine\\ORM\\PersistentCollection.
When I add the roles to the user's (un)serialize methods everything seems to be fine inside this isEqualTo method. He finally is grabbing the roles and I can add my logic to it. Fine! But afterwards Symfony is throwing erros like this or that. In my pov it has something to do with the serialization.
After some readings I've added serialization to the role entity because this seems the standard way to go. Serializing the user and the roles on their own, not (un)serializing the roles inside the user class. But as soon as I'm removing the roles from the user's serializing methods the old problem occurs again and again concerning $this->roles is always null when isEqualTo is fired. Everytime before and after everything's great, except this method call.
I haven't any clue what's exactly going wrong here.
Any idea?
Most likely this is caused by $this->roles being not populated at the moment of getRoles() call. It's hard to say what exactly is causing it without going through your other code. An easy solution would be to add a check to your getRoles() method at line 138:
if ($this->roles === null) {
return null;
}
But I'm not sure that's what you want to do, you probably want to figure out why roles are actually empty at that moment.