asciisd / zoho

Zoho package for Laravel
36 stars 37 forks source link

Column `'zohoable_id'` cannot be null #12

Closed ibmgeniuz closed 3 years ago

ibmgeniuz commented 3 years ago

@aemaddin I get Column 'zohoable_id' cannot be null when I try to use $leads->createOrUpdateZohoId($id);. Will there be any reason this is the case? Is that autogenerated or there is a way to set zohoable_id for each model.

Thank you

aemaddin commented 3 years ago

please can you send me the code sample for how you use the method, and also the exception trace.

ibmgeniuz commented 3 years ago

please can you send me the code sample for how you use the method, and also the exception trace.

Route::get('zoho/test/modules', function (){
    $leads = new App\Models\ZohoLead();
    $leads->createOrUpdateZohoId('VALID_ID_FROM_ZOHO_HERE');
});

Then I visited the link zoho/test/modules with postman. That's when I got this error. When I used tinker, it tells me that there's no table as in ISSUE . Postman works better but when the data is being saved, I get this problem.

aemaddin commented 3 years ago

createOrUpdate method is used to createOrUpdate CRM record, so you can used it after created a new Laravel model and you need to send it to CRM and save the CRM record id,

so this method can be used with Laravel model, for ex:

$user = App\Models\User::find(1);
$user->createOrUpdateZohoId();

if you leave $id empty or null, in this case zohobale will look into zohoable database to find this record and use it's CRM id, in case it's not exists it will try to find the record on Zoho CRM by searchByCriteria, finally if is not exists it will be create a new record on the Zoho CRM, return the new record id and save it on zohoable table.

ibmgeniuz commented 3 years ago

The exception I got was this. I think it was rather looking for a value for zohoable_id but couldn't find any. Can you please tell me how the code generates that or how I could generate from my model? I try the creating() static boot method but because it is not really using the Laravel Model directly, I can not change the data before it is saved.

{
    "message": "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'zohoable_id' cannot be null (SQL: insert into `zohos` (`zoho_id`, `zohoable_id`, `zohoable_type`, `updated_at`, `created_at`) values (REPLACE_WITH_VALID_ID_FROM_ZOHO_HERE, ?, App\\Models\\ZohoLead, 2021-01-14 12:59:57, 2021-01-14 12:59:57))",
    "exception": "Illuminate\\Database\\QueryException",
aemaddin commented 3 years ago

try this https://github.com/asciisd/zoho/issues/11#issuecomment-760249703

aemaddin commented 3 years ago

also you need to fill searchByCriteria method to get the record from CRM in case of updating or search if it's exists.

finally you need to fill zohoMandatoryFields method because it's the map method that will used when you create a new record into Zoho CRM

ibmgeniuz commented 3 years ago

try this #11 (comment)

I already changed it by this recommendation but still I get the

The exception I got was this. I think it was rather looking for a value for zohoable_id but couldn't find any. Can you please tell me how the code generates that or how I could generate from my model? I try the creating() static boot method but because it is not really using the Laravel Model directly, I can not change the data before it is saved.

{
    "message": "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'zohoable_id' cannot be null (SQL: insert into `zohos` (`zoho_id`, `zohoable_id`, `zohoable_type`, `updated_at`, `created_at`) values (REPLACE_WITH_VALID_ID_FROM_ZOHO_HERE, ?, App\\Models\\ZohoLead, 2021-01-14 12:59:57, 2021-01-14 12:59:57))",
    "exception": "Illuminate\\Database\\QueryException",

I know I can try to update the migration for zohos table to make zohoable_id nullable which could make the code work, but I don't know how that will affect other parts of the package. I'm trying not to hack my way through. Can that cause any issues?

This is my current code:

<?php

namespace Modules\Zoho\Models\Api;

use Asciisd\Zoho\CriteriaBuilder;
use Asciisd\Zoho\Zohoable;

class Lead extends Zohoable
{
    protected $zoho_module_name = 'Leads';

    public function searchCriteria()
    {
        // TODO: Implement searchCriteria() method.
    }

    public function zohoMandatoryFields()
    {
        // TODO: Implement zohoMandatoryFields() method.
    }
}
ibmgeniuz commented 3 years ago

My currency code has

class Lead extends Zohoable
{
    protected $zoho_module_name = 'Leads';

    public function searchCriteria()
    {
        return CriteriaBuilder::where('Email', $this->Email)
            ->toString();
    }

    public function zohoMandatoryFields()
    {
        return ['Email' => $this->Email];
    }

@aemaddin I think that the problem is with saving to the Local Database and not to Zoho CRM

aemaddin commented 3 years ago

ok I am trying to replicate your example and will get back to you.

aemaddin commented 3 years ago

@ibmgeniuz as you can see here:-

Screen Shot 2021-01-14 at 7 50 13 PM

I can create Zoho instance from my model without any problems,

so can you make sure that your model table is already exists.

Thanks

ibmgeniuz commented 3 years ago

Thank you @aemaddin . Works now 🥇 👍🏾 💯 . I didn't know that I needed to create ZohoLead table too, to hold the data I want to save in the database, that will be automatically attached to Zoho. If you do not mind, I could update a part of the read me and send a pull request to clarify on this for users who may experience same issues in future. Or you may try to clarify that in your read me.

My current working test code:

$leads = new ZohoLead(); // I have Zohoable model with zoho_leads Database table
//    New Lead 
    $attr = [
        'Email' => 'test@email.com',
    ];
    try {
        $zohoLead = $leads->findByZohoEmail($attr['Email']); //Check Zoho if Lead exists
        $zohoLead_id = $zohoLead->getEntityId(); // If exists get ID
        $zohoLead = $zohoLead->getData(); // Get Data from Zoho to Update in Database (NB: My DB columns are same as Zoho API columns. Mutate to suit your local database conventions)
        $new = $leads->updateOrCreate([
            'Email' => $zohoLead['Email'],
        ], $zohoLead); // Save or Update DB with new values checking against Email
       return  $new->createOrUpdateZohoId($zohoLead_id); // Create or Update Zohoable Data if not exist.
    } catch (ZCRMException $e) {
        $new = $leads->updateOrCreate([
            'Email' => $attr['Email']
        ], $attr);
        try{
            return $new->createAsZohoable($attr)->getData();
        } catch (InvalidZohoable $e) {
            $new->deleteZohoId();
            return $new->createAsZohoable($attr)->getData();
        }
    }

I used the tryCatch around my functions to ensure that any break will not stop the remaining codes from executing. Using ifElse will break the code the moment an Exception is thrown.

aemaddin commented 3 years ago

yes please if you send PR that will be great

Thanks.

ibmgeniuz commented 3 years ago

Hello, @aemaddin I have created a Pull Request. Please check and let me know if it is okay with you. In case I get to find out more ways people could use this package as I develop, I will add them to the docs. This package is amazing and very well written.

Thank you.