skip to Main Content

I need to copy description of the product from one table to another, everything works fine until the <img src="url" width="50%" height="50%" /> tag appears in the description. I have a script that copying descriptions of product one by one, with this script in cycle:

$rowParse = Parsing::find()->where(['art_post' => $value])->one();
$desc = $rowParse->description;
Yii::$app->db->createCommand("UPDATE `products` SET `description`=:desc WHERE `id_post`=:id && `art_post`=:art")
->bindValues([':desc'=>$desc, ':id'=>$id_post, ':art'=>$art_post])->execute();

If I do copying description of single product it works as needed – tag inserts with his attributes, but if i do, for example, 100 products in cycle, attributes width and height just disappears and it inserts just like <img src="url" />

Insertion of all other html-tags like span, strong, td, tr, p works normal.

I’m new to programming world, so I need help of big guys 😀
Thank you in advance

2

Answers


  1. Ensure that the description field in the products table has a data type that supports storing HTML, such as TEXT or LONGTEXT. If the field is of a type like VARCHAR with a limited length, it might truncate your HTML content.

    Make sure that when you retrieve the description from the Parsing table, the HTML content is not being encoded or escaped. Yii might automatically escape HTML entities when retrieving data from the database. You can use
    Html::decode() to ensure that the HTML is not encoded:

     $desc = Html::decode($rowParse->description);
    

    If you are updating a large number of records in a loop, you might want to consider wrapping your updates in a transaction to ensure data consistency:

     $transaction = Yii::$app->db->beginTransaction();
     try {
         // Your loop and update logic here
    
         $transaction->commit();
     } catch (Exception $e) {
         $transaction->rollBack();
         throw $e;
     }
    
    This way, if an error occurs during the loop, the changes are rolled back, and your data remains consistent.
    

    Add some logging statements to your code to see what values are being retrieved and updated. This can help you identify where the issue is occurring.

     Yii::info('Description from Parsing table: ' . $desc, 'app');
    

    You can then check the logs to see if the width and height attributes are present at the time of update.

    Login or Signup to reply.
  2. There is no need to read the values into PHP/Yii, and then write them back to the database, unless you are doing some complex manipulation in between. MySQL can do this for you much more efficiently.

    It is not clear from your question what the values of $value, $id_post and $art_post are or where they are coming from, but it seems likely this can be accomplished with a multi-table update. Something like:

    $sql = <<<'SQL'
        UPDATE products AS pr
        JOIN parsing AS pa ON pr.art_post = pa.art_post
        SET pr.description = pa.description
        WHERE pa.art_post = :art_post
        SQL;
    
    Yii::$app->db->createCommand($sql)
        ->bindValues([':art_post' => $value])
        ->execute();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search