Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast array stores the entire model as json #24053

Closed
mrbarletta opened this issue Apr 30, 2018 · 11 comments
Closed

Cast array stores the entire model as json #24053

mrbarletta opened this issue Apr 30, 2018 · 11 comments

Comments

@mrbarletta
Copy link

  • Laravel Version: #.#.# 5.6.
  • PHP Version: 7.2
  • Database Driver & Version: mysql 5.7

Description:

Doing this:

$modelItem= Model::create($exampleArray);
                        $modelItem->update(['contacts_details',['123'=>'321']]);
                        $modelItem->save();

Causes the json stored in the DB to be the full $exampleArray instead of the json representation of ['123'=>'321']

protected $casts = [
		'contacts_details' => 'array'
	];

I am missing something? Created this laravel project with the latest laravel installer available today.

Steps To Reproduce:

@bbashy
Copy link
Contributor

bbashy commented Apr 30, 2018

Can you update a column you've just saved? Try getting a new instance of that model after the create part.

@staudenmeir
Copy link
Contributor

staudenmeir commented Apr 30, 2018

You have to use $modelItem->update(['contacts_details' => ['123'=>'321']]);.

Also: $modelItem->save(); isn't necessary, update() already saves the changes to the database.

@mrbarletta
Copy link
Author

Thanks for your prompt answer, but the result is the same! so weird

$modelItem= Model::find($modelItem->id);
                        $modelItem->update(['contacts_details'=>['123'=>'321']]);

Could be something on the model itself?

<?php

/**
 * Created by Reliese Model.
 * Date: Mon, 30 Apr 2018 02:25:35 +0000.
 */

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;

/**
 * Class ZohoInvoice
 *
 * @property int $id
 * @property \Carbon\Carbon $date_entered
 * @property string $customer_name
 * @property string $status
 * @property string $fe_status
 * @property string $fe_tipo
 * @property string $fe_consecutivo
 * @property string $factura_recurrente_id
 * @property string $customer_zoho_id
 * @property string $customer_cedula
 * @property int $balance
 * @property string $currency_code
 * @property int $total
 * @property int $tax
 * @property int $line_number
 * @property float $line_item_quantity
 * @property string $line_item_name
 * @property string $line_item_description
 * @property float $line_item_rate
 * @property float $line_item_discount_amount
 * @property float $line_item_tax_percentage
 * @property float $line_item_item_total
 * @property int $zoho_invoice_id
 * @property string $billing_address_street2
 * @property string $billing_address_street
 * @property string $billing_address_city
 * @property string $billing_address_state
 * @property array $contacts_details
 *
 * @package App\Models
 */
class ZohoInvoice extends Eloquent
{
	public $timestamps = false;
	protected $table = 'zohoInvoices';
	protected $casts = [
		'balance' => 'int',
		'total' => 'int',
		'tax' => 'int',
		'line_number' => 'int',
		'line_item_quantity' => 'float',
		'line_item_rate' => 'float',
		'line_item_discount_amount' => 'float',
		'line_item_tax_percentage' => 'float',
		'line_item_item_total' => 'float',
		'zoho_invoice_id' => 'int',
		'contacts_details' => 'array'
	];

	protected $dates = [
		'date_entered'
	];

	protected $fillable = [
		'date_entered',
		'customer_name',
		'status',
		'fe_status',
		'fe_tipo',
		'fe_consecutivo',
		'factura_recurrente_id',
		'customer_zoho_id',
		'customer_cedula',
		'balance',
		'currency_code',
		'total',
		'tax',
		'line_number',
		'line_item_quantity',
		'line_item_name',
		'line_item_description',
		'line_item_rate',
		'line_item_discount_amount',
		'line_item_tax_percentage',
		'line_item_item_total',
		'zoho_invoice_id',
		'billing_address_street2',
		'billing_address_street',
		'billing_address_city',
		'billing_address_state',
		'contacts_details'
	];
}

@staudenmeir
Copy link
Contributor

Please run this and post the result (fully expanded):

\DB::enableQueryLog();
$modelItem= Model::find($modelItem->id);
$modelItem->update(['contacts_details'=>['123'=>'321']]);
dd(\DB::getQueryLog());

@mrbarletta
Copy link
Author

Here it is collapsed to avoid spam.
thanks again :)

Click to show getQueryLog results

Array
(
    [0] => Array
        (
            [query] => select * from `zohoInvoices` where `zohoInvoices`.`id` = ? limit 1
            [bindings] => Array
                (
                    [0] => 14
                )

            [time] => 2.28
        )

    [1] => Array
        (
            [query] => update `zohoInvoices` set `contacts_details` = ? where `id` = ?
            [bindings] => Array
                (
                    [0] => {"id":14,"date_entered":"2018-04-30 08:16:17","customer_name":"Cristian Uma\u00f1a","status":"draft","fe_status":"new","fe_tipo":"factura","fe_consecutivo":null,"factura_recurrente_id":"","customer_zoho_id":"229894000000053523","customer_cedula":"0205460677","balance":1234567952,"currency_code":"CRC","total":1234567952,"tax":0,"line_number":0,"line_item_quantity":500,"line_item_name":"Contrato de Servicios por Mantenimiento de CRM ","line_item_description":"descripcion del articulo","line_item_rate":0.12346,"line_item_discount_amount":0,"line_item_tax_percentage":0,"line_item_item_total":61.73,"zoho_invoice_id":229894000000635041,"billing_address_street2":"","billing_address_street":"","billing_address_city":"","billing_address_state":"","contacts_details":{"tax":0,"total":1234567951,"status":"draft","balance":1234567951,"fe_tipo":"factura","fe_status":"new","line_number":0,"currency_code":"CRC","customer_name":"Cristian Uma\u00f1a","line_item_name":"Contrato de Servicios por Mantenimiento de CRM ","line_item_rate":0.12346,"customer_cedula":"0205460677","zoho_invoice_id":229894000000635041,"customer_zoho_id":"229894000000053523","line_item_quantity":500,"billing_address_city":"","line_item_item_total":61.73,"billing_address_state":"","factura_recurrente_id":"","line_item_description":"descripcion del articulo","billing_address_street":"","billing_address_street2":"","line_item_tax_percentage":0,"line_item_discount_amount":0}}
                    [1] => 14
                )

            [time] => 11.49
        )

)


2018-04-30 14:16:17 /home/mrb/public_html/zohoInvoices/FacturaElectronicaCR/lxFEzoho/app/Http/Controllers/InvoiceController.php:105 -- Array
(
    [0] => Array
        (
            [query] => select * from `zohoInvoices` where `zohoInvoices`.`id` = ? limit 1
            [bindings] => Array
                (
                    [0] => 14
                )

            [time] => 2.28
        )

    [1] => Array
        (
            [query] => update `zohoInvoices` set `contacts_details` = ? where `id` = ?
            [bindings] => Array
                (
                    [0] => {"id":14,"date_entered":"2018-04-30 08:16:17","customer_name":"Cristian Uma\u00f1a","status":"draft","fe_status":"new","fe_tipo":"factura","fe_consecutivo":null,"factura_recurrente_id":"","customer_zoho_id":"229894000000053523","customer_cedula":"0205460677","balance":1234567952,"currency_code":"CRC","total":1234567952,"tax":0,"line_number":0,"line_item_quantity":500,"line_item_name":"Contrato de Servicios por Mantenimiento de CRM ","line_item_description":"descripcion del articulo","line_item_rate":0.12346,"line_item_discount_amount":0,"line_item_tax_percentage":0,"line_item_item_total":61.73,"zoho_invoice_id":229894000000635041,"billing_address_street2":"","billing_address_street":"","billing_address_city":"","billing_address_state":"","contacts_details":{"tax":0,"total":1234567951,"status":"draft","balance":1234567951,"fe_tipo":"factura","fe_status":"new","line_number":0,"currency_code":"CRC","customer_name":"Cristian Uma\u00f1a","line_item_name":"Contrato de Servicios por Mantenimiento de CRM ","line_item_rate":0.12346,"customer_cedula":"0205460677","zoho_invoice_id":229894000000635041,"customer_zoho_id":"229894000000053523","line_item_quantity":500,"billing_address_city":"","line_item_item_total":61.73,"billing_address_state":"","factura_recurrente_id":"","line_item_description":"descripcion del articulo","billing_address_street":"","billing_address_street2":"","line_item_tax_percentage":0,"line_item_discount_amount":0}}
                    [1] => 14
                )

            [time] => 11.49
        )

    [2] => Array
        (
            [query] => insert into `zohoInvoices` (`customer_name`, `status`, `fe_status`, `fe_tipo`, `factura_recurrente_id`, `customer_zoho_id`, `customer_cedula`, `balance`, `currency_code`, `total`, `tax`, `line_number`, `line_item_quantity`, `line_item_name`, `line_item_description`, `line_item_rate`, `line_item_discount_amount`, `line_item_tax_percentage`, `line_item_item_total`, `zoho_invoice_id`, `billing_address_street`, `billing_address_street2`, `billing_address_city`, `billing_address_state`, `contacts_details`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            [bindings] => Array
                (
                    [0] => Cristian Umaña
                    [1] => draft
                    [2] => new
                    [3] => factura
                    [4] => 
                    [5] => 229894000000053523
                    [6] => 0205460677
                    [7] => 1234567951.85
                    [8] => CRC
                    [9] => 1234567951.85
                    [10] => 0
                    [11] => 1
                    [12] => 1
                    [13] => Desarrollo de 4 formularios para página web
                    [14] => 
                    [15] => 1234567890.12346
                    [16] => 0
                    [17] => 0
                    [18] => 1234567890.12
                    [19] => 229894000000635041
                    [20] => 
                    [21] => 
                    [22] => 
                    [23] => 
                    [24] => {"customer_name":"Cristian Uma\u00f1a","status":"draft","fe_status":"new","fe_tipo":"factura","factura_recurrente_id":"","customer_zoho_id":"229894000000053523","customer_cedula":"0205460677","balance":1234567951,"currency_code":"CRC","total":1234567951,"tax":0,"line_number":1,"line_item_quantity":1,"line_item_name":"Desarrollo de 4 formularios para p\u00e1gina web","line_item_description":"","line_item_rate":1234567890.12346,"line_item_discount_amount":0,"line_item_tax_percentage":0,"line_item_item_total":1234567890.12,"zoho_invoice_id":229894000000635041,"billing_address_street":"","billing_address_street2":"","billing_address_city":"","billing_address_state":""}
                )

            [time] => 5.79
        )

    [3] => Array
        (
            [query] => select * from `zohoInvoices` where `zohoInvoices`.`id` = ? limit 1
            [bindings] => Array
                (
                    [0] => 15
                )

            [time] => 1.4
        )

    [4] => Array
        (
            [query] => update `zohoInvoices` set `contacts_details` = ? where `id` = ?
            [bindings] => Array
                (
                    [0] => {"id":15,"date_entered":"2018-04-30 08:16:17","customer_name":"Cristian Uma\u00f1a","status":"draft","fe_status":"new","fe_tipo":"factura","fe_consecutivo":null,"factura_recurrente_id":"","customer_zoho_id":"229894000000053523","customer_cedula":"0205460677","balance":1234567952,"currency_code":"CRC","total":1234567952,"tax":0,"line_number":1,"line_item_quantity":1,"line_item_name":"Desarrollo de 4 formularios para p\u00e1gina web","line_item_description":"","line_item_rate":1234567890.12346,"line_item_discount_amount":0,"line_item_tax_percentage":0,"line_item_item_total":1234567890.12,"zoho_invoice_id":229894000000635041,"billing_address_street2":"","billing_address_street":"","billing_address_city":"","billing_address_state":"","contacts_details":{"tax":0,"total":1234567951,"status":"draft","balance":1234567951,"fe_tipo":"factura","fe_status":"new","line_number":1,"currency_code":"CRC","customer_name":"Cristian Uma\u00f1a","line_item_name":"Desarrollo de 4 formularios para p\u00e1gina web","line_item_rate":1234567890.12346,"customer_cedula":"0205460677","zoho_invoice_id":229894000000635041,"customer_zoho_id":"229894000000053523","line_item_quantity":1,"billing_address_city":"","line_item_item_total":1234567890.12,"billing_address_state":"","factura_recurrente_id":"","line_item_description":"","billing_address_street":"","billing_address_street2":"","line_item_tax_percentage":0,"line_item_discount_amount":0}}
                    [1] => 15
                )

            [time] => 9.83
        )

)


@staudenmeir
Copy link
Contributor

Do you have any accessors/mutators in your model?

@mrbarletta
Copy link
Author

Not that I am aware of, but new here coming from symfony, I posted the model in question, were should I look for?

Very fresh project with a single controller, 1 route, + used reliese/laravel php artisan code:models to create the models for a 3 table DB.

@staudenmeir
Copy link
Contributor

You posted the model's whole code?

If you upload your project somewhere (e.g. Dropbox), I'll take a look at it. Or you debug Illuminate\Database\Eloquent\Model::update().

@mrbarletta
Copy link
Author

Thanks @staudenmeir

The tgz is here http://dev2.lionix.com/laravelissue.tgz

I only left the failing code with a demo JSON hardcoded.
To test it a POST /api/invoice is required.

There is a simple backup in the root folder called zohoInvoices.dump.sql. The database settings .env and config/database will have to match your database.

thanks again for looking into this despite tillkruss confidence that is my fault lol

@staudenmeir
Copy link
Contributor

That was a tricky one, TLDR: It's the fault of the reliese/laravel package. I'll create an issue over there.

Two possible workarounds:

@mrbarletta
Copy link
Author

awesome @staudenmeir - I am really grateful! 👍

I wouldn't be able to nail it as fast as you, thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants