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?
Related
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.
I've got a script that fetches data from a database using doctrine. Sometimes it needs to fetch the data for the same entity, the second time however it uses the identity map and therefor might go out of sync with the database (another process can modify the entities in the db). One solution that we tried was to set the query hint Query::HINT_REFRESH before we run the DQL query. We however would like to use it also with simple findBy(..) calls but that doesn't seem to work? We would also like to be able to set it globally per process so that all the doctrine SELECT queries that are run in that context would actually fetch the entities from the DB. We tried to set the $em->getConfiguration()->setDefaultQueryHint(Query::HINT_REFRESH, true); but again that doesn't seem to work?
Doctrine explicitly warns you that it is not meant to be used without a cache.
However if want to ignore this, then Cerad's comment (also mentioned in in this answer) sound right. If you want to do it on every query though you might look into hooking into a doctrine event, unfortunately there is no event for preLoad, only postLoad, but if you really don't care about performance you could create a postLoad listener which first gets the class and id of the loaded entity, calls clear on the entity manager and finally reloads it. Sounds very wrong to me though, I wash my hands of it :-)
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.
I am writing my unit tests with phpunit to Laravel application. I am using Eloquent and Woodling library. I want to test many to many relationship.
I have Users table and Friends table. Everything worked, when I tested it manually. I am able to add friends. I wanted to test this functionality.
I created blueprints and I call them with saved method like this.
$user = Woodling::saved('LonelyUser');
$user2 = Woodling::saved('LonelyUser2');
$users = User::all()->toArray();
var_dump($users);
$user->addFriend($user2);
I got a database constraint error in the last line, because the users were not persisted to database (I know that, because they are not in var_dump output).
If Woodling::saved does not persist to database, than what does it do? The docs say, that it calls save method on model. save method should persist the model to database.
What is the saved method purpose and how is it different than retrieve?
Woodling::saved persists to database correctly, but if something goes wrong during saving, it doesn't show any errors. In my case save method was actually Ardent::save, which does some validations. This validations did not pass and Model::save was never called.
I'm in the process of trying to create a logging interface for our application. I need to track when a change happens in the database through the application. So when someone updates a field I need to insert a row into the log with the table, columns original value, columns new value, timestamp, and user that made change. To me the logical way of doing this is to tie into the DB class in laravel so everytime it's called and an update / delete method is used it runs my new method of getting the needed info and inserting it in the log.
I need it to work at the DB level I believe as it needs to happen for updates / deletes called from DB or eloquent.
How would I go about doing this?
Eloquent provides you with some nice events, they're even in the docs ! Who knew you can find so much in there.
http://laravel.com/docs/eloquent#model-events
Instead of trying to attach something onto the DB layer, have you considered using Model Observers (http://laravel.com/docs/eloquent#model-observers) to watch all update and delete events and put your logic in that observer?
Upon searching and trial and error I accomplished my goal the following way.
I created an Event listener that I for organizational purposes placed in /app/events/database.php
With the following
<?php
Event::listen('illuminate.query', function($query, $bindings, $time, $name)
{
// Code to log query goes here
});
I then placed in my /app/start/global.php the following line
include(app_path().'/events/database.php');
This now captures all requests to query the database using either DB, or Eloquent.