Replicate model with laravel generating incorrect sql - laravel-4

We are using laravel 4.2
I am replicating a model
$model = $model->replicate();
Then when I go to save the model, it is trying to create an sql statement based on the associative and indexed values of the array.
"0" index is there by itself as the column named after the primary key has been removed
Column '0' not found (SQL: insert into "obl" ("0", "1", "obl_id", "2", "obl_dscrptn", "3", "
We are using a custom database provider (sqlanywhere),
so I am not sure if the problem lies with that or laravel.
The replicate function seems to be un-documented, although I have seen a number of posts referencing it's use.
Has anyone else struck this issue?

I know this is years later, but the solution is to check your database.php config and make sure you set the correct fetch method:
'fetch' => PDO::FETCH_CLASS,

Related

Laravel - Automatically store history in column

Can anyone help me to automatically save the history of all changed columns in a model to a history column in the same table as JSON format with column(s) name, original value, changed to, changed by?
I am looking for Traits like centralized function to use in all models.
I am using Laravel 8 with PostgreSQL.
This answer is maybe outdated since it is Laravel 4. But if it works, it is quickest way to insert log in DB wherever save() method is called for that model.
https://stackoverflow.com/a/20694395/13893004

Why Laravel/Eloquent single object save() updates multiple records

I'm fetching a specific record with a DB table using
$myTableObj = MyTable::where(['type' => $sometype])->first();
Getting it successfully, updating some fields and saving with
$myTableObj->save();
Surprisingly, this record is updated along with another record that also has 'type' = $sometype. What can be done to prevent this?
NOTE: originally the table did not have the auto increment id field, but I have read in forums that it may make problems in Laravel so I did add it, which did not solve the problem.
Method save() working with 'id' filed only.
You can try this
$myTableObj = MyTable::where(['type' => $sometype])->update(['something' => 'value']);
Source
I understand update() is to update, but, my answer works fine and fits good for update too. Its useful where you dont want columns to be defined once again for update, (sp when they are not fillable, its tested with primary key as condition)
$myTableObj->save(); basically its for saving new record, if you want to update that row you can update like below code:
$myTableObj=new MyTable;
$myTableObj->exists=true;
$myTableObj->type=$sometype;//this is your condition, identify
$myTableObj->update();
I think what's happening here is Laravel is saving as well as updating row.

Slickgrid/Dataview without Primary Key

I am using SlickGrid/DataView for CRUD purposes. It is working fine. However, we have some of the tables which do not have primary key.
Q1: How can I use dataview for such tables? If not then am I left with using Slickgrid without dataview only OR I have another choice? Any example would be appreciated.
Q2: Does Dataview support composite primary keys? If yes, can anybody give me an example of using it?
thanks
SlickGrid requires at least the ID field, you can take whichever that could be named differently in your table and then alias it to "ID" but it has to be a UNIQUE id field because SlickGrid is using it for row naming and so on.
If you do not have a UNIQUE field and you really want to use composite primary keys, then I suggest you create a fake ID field which you simply increment by 1 each loop. A simple way is to do this while fetching the data, something similar to the following:
$sqlTable = "SELECT * FROM myTable";
$result = mysql_query($sqlTable,$connMySQL);
$i = 1;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$tabData[] = array(
"id" => $i++,
"column1" => $row['column1'],
"column2" => $row['column2']
);
}
Now if you do use a fake ID like I just said, this will give you some problems doing the CRUD (update, delete) since your ID is not real and point to nowhere... but there is a way to fix that too. By the way, you should split your question into 2 separate question since they are different subjects. I suggest you ask your CRUD subject in another question and that you provide some code so we can develop from there.

Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?

Assume, you are trying to create a new user, with a User model ( using soft deletes ) having a unique rule for it's email address, but there exists a trashed user within the database.
When trying to validate the new user's data, you will get a validation error, because of the existing email.
I made some kind of extra validation within my Controllers, but wouldn't it be nice to have it all within the Model?
Would you suggest creating a custom validation rule?
As I haven't found a clean solution now, I am interessted in how others solved this problem.
You can validate passing extra conditions:
'unique:users,deleted_at,NULL'
This sounds like an issue with your business logic rather than a technical problem.
The purpose of a soft delete is to allow for the possibility that the soft-deleted record may be restored in the future. However, if your application needs uniqueness of email (which is completely normal), you wouldn't want to both create a new user with that email address and be able to restore the old one as this would contravene the uniqueness requirement.
So if there is a soft deleted record with the email address for which you are adding as a new record, you should consider instead restoring the original record and applying the new information to it as an update, rather than trying to circumvent the uniqueness check to create a new record.
This is the best approach
'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL',
It give you this query
select count(*) as aggregate from `users` where `email` = ? and `deleted_at` is null
Laravel offers "Additional Where Clauses".
My url validation rule (from the update model method) looks like this:
$rules['url'] = 'required|unique:pages,url,'.$page->id.',id,deleted_at,NULL';
This means that the url must be unique, must ignore the current page and ignore the pages where deleted_at id not NULL.
Hope this helps.
Your Eloquent model should have the $softDeletes property set. If so, then when you perform a WHERE check, like User::where('username', 'jimbob'), Eloquent will automatically add in the query WHERE deleted_at IS NULL... which excludes soft deleted items.
In laravel 6.2+ you can use
'email' => ['required', Rule::unique('users')->whereNull('deleted_at')]

Magento eav entity setup failing - Can't create table

I am trying to set up a custom entity in Magento 1.7.0.0, following alan storms article about it, but with this simple install script, it tells me that "Can't create table: eavblog_posts".
My install script is deadsimple and looks like this:
<?php
$installer = $this;
$installer->addEntityType('complexworld_eavblogpost',
Array(
'entity_model'=>'complexworld/eavblogpost',
'attribute_model'=>'',
'table'=>'complexworld/eavblogpost',
'increment_model'=>'',eav/entity_increment_numeric
'increment_per_store'=>'0'
));
$installer->createEntityTables(
$this->getTable('complexworld/eavblogpost')
);
How can i get my install script working? Is this a known magento bug?
In my case, the issue was due to a bug with the createEntityTables() method in Magento 1.7/1.12
In their answer to this bug report, the Magento team recommend commenting out line 417 from lib/Varien/Db/Adapter/Pdo/Mysql.php:
$this->_checkDdlTransaction($sql);
Instead, I would recommend following the advice in Zachary Schuessler's post and either
1) copying the createEntityTables() method to your own file (Your/Module/Model/Resource/Setup.php) and commenting out the transaction methods...
or
2) writing an abstracted query in order to keep transactions:
// Example of MySQL API
/**
* Create table array('catalog/product', 'decimal')
*/
$table = $installer->getConnection()
->newTable($installer->getTable(array('catalog/product', 'decimal')))
->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'identity' => true,
'nullable' => false,
'primary' => true,
), 'Value ID')
->addColumn(...
First of all, this line is wrong:
'increment_model'=>'',eav/entity_increment_numeric
It needs to be inside the quotes.
Failing that there are some bugs in the installer functions of the latest version.
Go into your database using phpMyAdmin or similar and check to see if any of the tables already exist. If they do, delete them. Also delete the module's record in core_resource.
Try again.
Then there's a step in here I cant remember off the top of my head (useful, I know, but I'll try and remember it tonight and edit this).
Once the tables have been created, if you look at the foreign key assignments for the type tables (int, text char etc) you'll notice that the entity_id field is looking at eav_entity.entity_id. This needs to change to your eavblogpost_entity table.
You might also notice that the eavblogpost_entity.entity_id field is INT(11), when all the foreign key references are INT(10). Change the eavblogpost_entity.entity_id field to INT(10) manually.
The only way around all of this is to override the createEntityTables() function with one that works, or create all the tables manually. Here's a good resource to help you through that parthttp://inchoo.net/ecommerce/magento/creating-an-eav-based-models-in-magento/
Mess around with all of these as you go and I'm sure you'll stumble across the step that you have to do that I've forgotten. Sorry!

Resources