after creating, default value set in DB is not reflected?

I have a table in MariaDB configured like this:

CREATE TABLE orders (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
...
    createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    createdBy INT UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (id)
);

I called create () after setting the values except id, createdAt, createdBy. The result was true. id returns the correct value. After that, setting a value in another field and update() will return false. The error is "PresenceOf" and it says "createdAt is required", "createdBy is required".

like this:

$odr = new Models\Orders();
$odr->someField = 'foo';
$result = $odr->create();  // return true
$id = $odr->id;  // get valid value
$odr->anotherField = 'bar';
$result = $odr->update();  // return false

After create(), a valid value is set in id. Is the value not reflected in other than that? (Values that have not been explicitly set remain null) Is it necessary to re-acquire by using firstFirst(), etc. in order to match the value on the table (= default value is reflected)?

edited Apr '20

You may need to do just that. refresh() is what you'd want rather than findFirst(). They would both do the same work in this context, but refresh() seems a bit more semantic for this.



3.4k

I see. Thank you for your suggestion.

I will use refresh() that you taught me when it is not a fixed value. In the case of a fixed value, I saw how to set it as an initial value when declaring a property, but how is that kind of usage?

    / **
     *
     * @var integer
     * /
    protected $createdBy = 0;

and ...

I am not good at English so I would like you to teach me. Does that "semantic" mean effective or meaningful?

I don't think declaring the variable as an object property will have any effect on how Phalcon updates the object. Personally, I never declare those properties.

Yes, you've got a good grasp on what "semantic" means. I mean that the word "refresh" is more accurate at describing what you're doing that "findFirst". The word itself gives a little clue to what you're doing.

The term "semantic" is used a lot in web design. For instance: if you've got a page with some navigation, one should use the <nav> tag rather than a <div> tag, as <nav> is more semantic - it more accurately describes what the element is being used for.



3.4k
edited Apr '20

I was relieved that my understanding of "semantic" was correct. The point is to make what I am trying to do as clear as possible.

I think this way.

When null is given in the table definition and the default value is set, if the property is initialized to the default value when you do new, there will be no inconsistency at the first save (). And even if you save () after changing some properties, no problem occurs. The "default value" is stored unless it is overwritten. (It's a pity that it seems that it will not work unless it is initialized with a simple value).

However, there is one question.

public function initialize()
    {
        $this->skipAttributes(['createdAt', 'updatedAt']);
        $this->useDynamicUpdate(true);
    }

I try that. The column that has not been updated (createdBy in the above example) is pointed out as "createdBy is required". Since it has not been rewritten since new, it should be null all the time. At the time of saving, it may not have been successfully deployed to the execution environment. I haven't looked it up yet.

You're not skipping the createdBy attribute. Adding that may fix your problem.

Also, I edited your post to enable php syntax highlighting. That can be done by adding "php" after the 3 backticks. You can also invoke volt,sql,html,css and probably a few others too.



3.4k

I don't always want to skip the createdBy attribute. If you add it to skipAttributes, it will be the default value even if you explicitly give createdBy.

Thank you very much for the syntax highlights.

edited Apr '20

This makes that answer appear right after the question so it's easier to find within a thread. It also helps us unmark the question as unanswered so we can have more eyes helping others with open questions upsers portal



3.4k

He did not agree to initialize the property. However, I think the proposed way (= skipAttributes ) can only set default values of the table definition. So I was waiting for another suggestion.

Should I close ?

I'm not sure what Atkinsi's talking about - my guess is it's a spam bot. Don't worry about closing if the thread's not answered.

After the first query in your initial post, was the database populated as you'd expect?



3.4k

Yes. The insert was successful without any problems and is reflected in the database. I think that the initial value of the property and the default value of the table definition are the same, so I get the same result without passing null. This means that it is not necessary to write a default value in the table definition, but since the meaning is clear when looking at the table alone, it is better not to omit it.

Of course, there is no problem with the second update.

If possible, I think it would be nice to reflect the default value of the table definition in the initial value of the property when building the model with webtools.

Or it may be good to refresh() by adding a flag when creating().

edited May '20

You can use SQL Server Management Studio to specify a default value that will be entered into the table column. You can set a default by using the Object Explorer of the user interface or by submitting Transact-SQL mcdvoice.com



3.4k

Hi.

Thank you for your suggestions. But, I don't plan to use MS SQL, so I don't think I'll use your suggested tool.

I was waiting for a reply to the question that using skipAttributes could only be set to the default value at definition time. And as a response, I was waiting for an opinion on whether to write the same value as the DB definition as the default value in the property of the model class and to use refresh() immediately after create(). Since using refresh() tends to increase the load on the server, I think it should be avoided, so I also thought that it would be good to get a way to write a default value for the property. (I didn't get a reply ...)



3.4k

Hi I haven't fully understood it yet, but I'd be happy if it helped.

Hi.

Thank you for your suggestions. But, I don't plan to use MS SQL, so I don't think I'll use your suggested tool.

I was waiting for a reply to the question that using skipAttributes could only be set to the default value at definition time. And as a response, I was waiting for an opinion on whether to write the same value as the DB definition as the default value in the property of the model class and to use refresh() immediately after create(). Since using refresh() tends to increase the load on the server, I think it should be avoided, so I also thought that it would be good to get a way to write a default value for the property. (I didn't get a reply ...)

Thank you! This was a big help.