We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Skip rowcount before create or save method

Hi,

I would like to skip select rowcount before save or create method for model. Anyone have idea how to do it ?

It's returning me notice Notice: Undefined index: rowcount .

I am using below code.

$instance = new $model(); $instance->create($obj_data) // and $instance->save($obj_data)

thanks

I really doubt the code you posted is generating that error. Phalcon doesn't ask for "rowcount" before every insert/update statement. Somewhere else in your code you're referencing an array element by index "rowcount".

HI, we have developed custom adapter for cassandra pdo to work with phalcon.



309
edited Oct '14

@shivanshuit914: you're right, there's a SELECT COUNT(*) AS 'rowcount' that's run before any create/save/update. I found it's done on related records.



93

Anything about this? I control these things before and I don't want the model to check it again. I prefer to put a try catch and control the PDOException in the rare case of duplicity than a 'SELECT COUNT(*) "rowcount" FROM' before each 'INSERT'

The query is done before the 'beforeValidationOnCreate()' and I don't know how to skip this useless SELECTS



98.9k
edited Nov '14

How the ORM would know if the record exists and whether it must insert or update without a SELECT COUNT(*) "rowcount"?

Anything about this? I control these things before and I don't want the model to check it again. I prefer to put a try catch and control the PDOException in the rare case of duplicity than a 'SELECT COUNT(*) "rowcount" FROM' before each 'INSERT'

The query is done before the 'beforeValidationOnCreate()' and I don't know how to skip this useless SELECTS



93

As I understand, calling create() instead of save() or update() 'forces' the ORM to try an Insert https://docs.phalcon.io/en/latest/reference/models.html#create-update-with-confidence

How the ORM would know if the record exists and whether it must insert or update without a SELECT COUNT(*) "rowcount"?

Anything about this? I control these things before and I don't want the model to check it again. I prefer to put a try catch and control the PDOException in the rare case of duplicity than a 'SELECT COUNT(*) "rowcount" FROM' before each 'INSERT'

The query is done before the 'beforeValidationOnCreate()' and I don't know how to skip this useless SELECTS



98.9k

Calling create() does not force an insert, it just ensures than an update will not made if the record already exists:

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/model.zep#L2923



93

Yes, now I see that these methods always return boolean values.

But the comment "If the record already exists we must throw an exception" means "add an errorMessage to the model and return false"

Handling "errors" with the return value and getMessages () instead of catching PDOExceptions is nice, but the price is an additional query for each INSERT / UPDATE using save().
IMHO is not the philosophy that the Phalcon's ORM should follow but that's just my opinion.

The point is when I want to INSERT a row with a explicit PK id, the resulting SQL does two "rowcount" and then an insert:

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/model.zep#L2923

function create()
{
if (SELECT COUNT(*) "rowcount"....) return false
    else this->save()
}

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/model.zep#L2743

function save()
{

    if (SELECT COUNT(*) "rowcount"....) 
        self::OP_UPDATE
    else
        self::OP_INSERT

create() and update() checks if the row exists, then both call save() anyways where again checks if the row exists, so better just call save() in this situation The other alternative (within the framework) would be using PHQL

Thanks :)