Certain Database Table fields not saving using cakePHP - cakephp

I've added two new fields to my subscription database table namely first_name and last_name to table subscriptions
But whenever I get my values returned from the form and I try to save them then it's not saving to the database?
My code is as follows
$this->data['Subscription']['id'] = $subscription['Subscription']['id']; //To get the primary key
$this->data['Subscription']['first_name']; //Echoes out the name I entered
$this->data['Subscription']['last_name']; //Echoes out the surname I entered
Then I call $this->Subscription->save(); WHICH SAVES ALL OTHER FIELDS except the fields I've newly added to the table, I tried $this->Subscription->set($this->data['Subscription']); and also nothing wants to save?
I'm not sure what I'm doing wrong here? Any help will be higly appreciated!!!!

Assuming that you run the app with debug 0 you need to delete the cache in /tmp because the database schema is cached by CakePHP.

According to your description, you forgot to add $this->data to save, you must save your data like so:
$this->Subscription->save( $this->data );

Related

Laravel 5.2 update last created record

I am new to laravel, I have tried a bunch of recommendations but none have worked for what I need. Do to requirements for design I am splitting my registration into two tables artist and the normal user table. The problem is email verification is done through the artist table. After that process has been completed I would simply like to update the last created user in the user table from 0 to 1 in the active column.
I can not figure out how to get this to work.
return $this->users()->where('verified')->first()->verified()- >orderBy('created_at', 'desc')->first();
Then ran update didn't work
User::orderby('created_at', 'desc')->first();
Then tried update didn't work
$user = User::find(1)->verified()- >update(array(
'Verified' => '1'
));
Non of the above have worked. Any suggestions are appreciated.
Given your requirements, and the way you are doing things - you are creating a user during registration (in the users table and setting verified to 0) and then putting some of their information in an artists table until they are verified.
I would add a users_id column to your artists table and set the value of this to the id of the newly created user.
When they verify, you can find their information in your artists table using their user_id and then move it anywhere you need to.

Cakephp 3 habtm with additional data

While going through a cakephp 3 tutorial about bookmarks and tags I've struggled with a problem: I want to add the third field to the users_tags table (tag_type: important or not), but when it saves the data it rewrites previous values to the default database value of "tag_type".
Could you please help me figure out what I'm doing wrong?
If you added the tag_type column to the users_tags table after running bake commands, check $_accessible array value in entity file of the table i.e. src/Model/Entity/UsersTag.php and add an element with column name as key and true as value to make sure column value can be mass assigned
In your Form html of user add/edit page, add an input like this so that cakephp matches its value with proper column automatically when you create an entity in your controller :
echo $this->Form->input('tags.0._joinData.tag_type');

CakePHP Insert Statements

I am having trouble inserting data into a MySQL table using CakePHP. I am making a table that once a row is inserted, it should not be able to be overwritten. With the save method of CakePHP, if I try to enter something with the same primary key but different information, it will just update that entry instead of returning an error. I tried to unset the id for the model and also tried to do Model->create() before the call, but it still just overwrites the data.
Additionally, I have been trying to use the Model->query() method instead, but I cannot get it to properly check for errors. I want it to insert, but return an error message if the ID is already taken, so I tried this.
$insertQuery = ("INSERT INTO `students` VALUES ('{$id}', '{$lastname}', '{$firstname}', '')");
$this->Student->query($insertQuery) or die("error" .mysql_error());
However, the query command returns an array and not a truth value, so this will call die every time. I would appreciate any advice someone can give.
Your table should have only the Id as the primary key and it should be IDENTITY.
In CakePHP if you dont specify the Id in the form you create to submit your data it will create another record, even with the same values.
But you should revise your model to be sure that the id is not required or something like that.

CakePHP need to know model ID before saving

My situation:
I have a form based on several models linked together.
In background I have a routine for saving images , based on ajax call to a particular controller which uploads the image and displays it on a textarea in realtime.
My problem applies when I have to save the record first time, because the ajax routine needs to know the ID of the record to associate the image , but this will be created only after save() will be called.
Is there a way to obtain a ID before the model saves? or some other workaround
Thanks in advance to everyone
Rudy
I think #RichardAtHome provides a good input on this. I'll just add a third solution you may want to consider:
Use universally unique identifiers (UUID) for that model's primary key. CakePHP offers a way to generate them via the String utility. So what you would do is generate the id when the page loads with String::uuid() and then send it as part of the ajax requests.
A couple of solutions:
Change your workflow, so that the images can only be linked to the main model once the main model has been saved (and has an id).
Don't save your images to the database until the main model has been created. You could store them in a session, or just keep appending fields to the end of the form as the user adds more fields.
Option1:
You could try using UUIDs instead of INTs. That way, you can just use CakePHP to generate an ID: String::uuid();.
After you create it, you can populate an id field that would then make the item save with that id.
I'm not sure I'd rely on it's uniqueness if I were doing banking software or something critical, but - it's something like 1 in a billion chance to be duplicate, and for normal websites/software, it seems like that would suffice.
Option2:
In a lot of our projects, we've found it helpful to have a very simple "create a Thing" page with just a title field, then, once saved, it takes them immediately to the more in-depth "edit" page where they can upload files, save extra data...etc etc. Almost like a pg1, pg2. That way, files, as well as any other dynamic ajax-driven data will have an ID to work with.
Probably not, because the ID will be set by the Database, and not CakePhp, and the id can only be known after the database has auto-incremented the ID field...
Not sure how your flow is, but I guess you can save a dummy/empty row to the database without all the associations when the controller gets the page (when $this->data is empty). You know have an ID to an empty row in the database that is like "12, NULL, NULL, NULL, NULL, NULL, ...". You can then store the ID of that dummy record (or do $this->set("id", $id) to pass it to the view to use in your ajax-calls).
After the POST of your page you can then use the ID to store all other information in dummy row, or delete the dummy row from the database when something fails/user navigates away...

Cake PHP revisioning data/id refresh

So here is what I am trying to do.
I have one form which will post data to two tables say table a and table b. Now when I edit the same record I want that data in one table should be updated and in other table it should create new row and add the data.
Can anyone tell me how to achieve this in cakephp
Thanks,
When saving, if the primary key (usually id) of the model is given, cakephp will update, otherwise it will insert.

Resources