1

the task is to save the $model->image with row id in it. to anticipate the next autoincrement id i use the query below. mysql returns right result, but when i try to save this query result in model it saves it like 'Array' string. For example : article_Array_1516534305.png instead of article_13_1516534305.png

i tried to use array_shift() function, but result is the same

        $connection = Yii::$app->getDb();
        $dbName = $this->getDsnAttribute('dbname', Yii::$app->getDb()->dsn);

        $query = new Query;
        $query->select('AUTO_INCREMENT')
            ->from('information_schema.TABLES')
            ->where("TABLE_SCHEMA = '".$dbName."'")
            ->andWhere("TABLE_NAME = '".$this->tableName()."'");
        $id = $query->one();

        $this->image = 'article_'.$id.'_'.time(). '.' . $image->extension;  

2 Answers 2

3

With $query->one() you get a single model

If you need only the column value you should use scalar()

$query->scalar();

http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html

http://www.yiiframework.com/doc-2.0/yii-db-query.html#scalar()-detail

or if you want the model however you can use

$id = $query->one(); 
$id = $id['AUTO_INCREMENT'];
Sign up to request clarification or add additional context in comments.

1 Comment

you arwe welcome .. anyway answer updated with the access to the model column too..
0

the problem was that i had associated array

$id = $query->one(); $id = $id['AUTO_INCREMENT']; now it works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.