<commit_before><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @category   Pimcore
 * @package    Object_Objectbrick
 * @copyright  Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 */

class Object_Objectbrick_Data_Resource extends Pimcore_Model_Resource_Abstract {


    /**
     * @var Object_Concrete_Resource_InheritanceHelper
     */
    protected $inheritanceHelper = null;    

    
    /**
     * create data rows for query table and for the store table
     *
     * @return void
     */
    protected function createDataRows($object) {
        try {
            $tableName = $this->model->getDefinition()->getTableName($object->getClass(), false);
            $this->db->insert($tableName, array("o_id" => $object->getId(), "fieldname" => $this->model->getFieldname()));
        }
        catch (Exception $e) {
        }

        try {
            $tableName = $this->model->getDefinition()->getTableName($object->getClass(), true);
            $this->db->insert($tableName, array("o_id" => $object->getId(), "fieldname" => $this->model->getFieldname()));
        }
        catch (Exception $e) {
        }
    }

    /**
     * @param Object_Concrete $object
     * @return void
     */
    public function save (Object_Concrete $object) {

        // HACK: set the pimcore admin mode to false to get the inherited values from parent if this source one is empty
        $inheritedValues = Object_Abstract::doGetInheritedValues();

        $storetable = $this->model->getDefinition()->getTableName($object->getClass(), false);
        $querytable = $this->model->getDefinition()->getTableName($object->getClass(), true);


        $this->inheritanceHelper = new Object_Concrete_Resource_InheritanceHelper($object->getClassId(), "o_id", $storetable, $querytable);

        $this->createDataRows($object);


        Object_Abstract::setGetInheritedValues(false);

        $fd = $this->model->getDefinition()->getFieldDefinitions();

        $data = array();
        $data["o_id"] = $object->getId();
        $data["fieldname"] = $this->model->getFieldname();

        // remove all relations
        try {
            $this->db->delete("object_relations_" . $object->getClassId(), "src_id = " . $object->getId() . " AND ownertype = 'objectbrick' AND ownername = '" . $this->model->getFieldname() . "' AND (position = '" . $this->model->getType() . "' OR position IS NULL OR position = '')");
        } catch(Exception $e) {
            Logger::warning("Error during removing old relations: " . $e);
        }

        foreach ($fd as $key => $value) {
            $getter = "get" . ucfirst($value->getName());
            
                if (method_exists($fd, "save")) {
                    // for fieldtypes which have their own save algorithm eg. objects, multihref, ...
                    $value->save($this->model);

                } else {
                if ($value->getColumnType()) {
                    if (is_array($value->getColumnType())) {
                        $insertDataArray = $value->getDataForResource($this->model->$getter(), $object);
                        $data = array_merge($data, $insertDataArray);
                    } else {
                        $insertData = $value->getDataForResource($this->model->$getter(), $object);
                        $data[$key] = $insertData;
                    }
                } else if (method_exists($value, "save")) {
                    // for fieldtypes which have their own save algorithm eg. fieldcollections
                    $value->save($this->model);
                }
            }
        }

        $this->db->update($storetable, $data, $this->db->quoteInto("o_id = ?", $object->getId()));


        // get data for query table 
        // $tableName = $this->model->getDefinition()->getTableName($object->getClass(), true);
        // this is special because we have to call each getter to get the inherited values from a possible parent object


        Object_Abstract::setGetInheritedValues(true);

        $objectVars = get_object_vars($this->model);

        $data = array();
        $data["o_id"] = $object->getId();
        $data["fieldname"] = $this->model->getFieldname();
        $this->inheritanceHelper->resetFieldsToCheck();
        $oldData = $this->db->fetchRow("SELECT * FROM " . $querytable . " WHERE o_id = ?", $object->getId());

        foreach ($objectVars as $key => $value) {
            $fd = $this->model->getDefinition()->getFieldDefinition($key);

            if ($fd) {
                if ($fd->getQueryColumnType()) {
                    //exclude untouchables if value is not an array - this means data has not been loaded

                    $method = "get" . $key;
                    $insertData = $fd->getDataForQueryResource($this->model->$method(), $object);

                    if (is_array($insertData)) {
                        $data = array_merge($data, $insertData);
                    }
                    else {
                        $data[$key] = $insertData;
                    }


                    //get changed fields for inheritance
                    if($fd->isRelationType()) {
                        if (is_array($insertData)) {
                            $doInsert = false;
                            foreach($insertData as $insertDataKey => $insertDataValue) {
                                if($oldData[$insertDataKey] != $insertDataValue) {
                                    $doInsert = true;
                                }
                            }

                            if($doInsert) {
                                $this->inheritanceHelper->addRelationToCheck($key, array_keys($insertData));
                            }
                        } else {
                            if($oldData[$key] != $insertData) {
                                $this->inheritanceHelper->addRelationToCheck($key);
                            }
                        }

                    } else {
                        if (is_array($insertData)) {
                            foreach($insertData as $insertDataKey => $insertDataValue) {
                                if($oldData[$insertDataKey] != $insertDataValue) {
                                    $this->inheritanceHelper->addFieldToCheck($insertDataKey);
                                }
                            }
                        } else {
                            if($oldData[$key] != $insertData) {
                                $this->inheritanceHelper->addFieldToCheck($key);
                            }
                        }
                    }

                }
            }
        }
        $this->db->update($querytable, $data, "o_id = " . $object->getId());

        $this->inheritanceHelper->doUpdate($object->getId());
        $this->inheritanceHelper->resetFieldsToCheck();

        // HACK: see a few lines above!
        Object_Abstract::setGetInheritedValues($inheritedValues);

    }
<commit_msg>Object Bricks: using insertOrUpdate instead of createDataRows (insert then update)<commit_after><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @category   Pimcore
 * @package    Object_Objectbrick
 * @copyright  Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 */

class Object_Objectbrick_Data_Resource extends Pimcore_Model_Resource_Abstract {


    /**
     * @var Object_Concrete_Resource_InheritanceHelper
     */
    protected $inheritanceHelper = null;    


    /**
     * @param Object_Concrete $object
     * @return void
     */
    public function save (Object_Concrete $object) {

        // HACK: set the pimcore admin mode to false to get the inherited values from parent if this source one is empty
        $inheritedValues = Object_Abstract::doGetInheritedValues();

        $storetable = $this->model->getDefinition()->getTableName($object->getClass(), false);
        $querytable = $this->model->getDefinition()->getTableName($object->getClass(), true);


        $this->inheritanceHelper = new Object_Concrete_Resource_InheritanceHelper($object->getClassId(), "o_id", $storetable, $querytable);

        Object_Abstract::setGetInheritedValues(false);

        $fd = $this->model->getDefinition()->getFieldDefinitions();

        $data = array();
        $data["o_id"] = $object->getId();
        $data["fieldname"] = $this->model->getFieldname();

        // remove all relations
        try {
            $this->db->delete("object_relations_" . $object->getClassId(), "src_id = " . $object->getId() . " AND ownertype = 'objectbrick' AND ownername = '" . $this->model->getFieldname() . "' AND (position = '" . $this->model->getType() . "' OR position IS NULL OR position = '')");
        } catch(Exception $e) {
            Logger::warning("Error during removing old relations: " . $e);
        }

        foreach ($fd as $key => $value) {
            $getter = "get" . ucfirst($value->getName());
            
                if (method_exists($fd, "save")) {
                    // for fieldtypes which have their own save algorithm eg. objects, multihref, ...
                    $value->save($this->model);

                } else {
                if ($value->getColumnType()) {
                    if (is_array($value->getColumnType())) {
                        $insertDataArray = $value->getDataForResource($this->model->$getter(), $object);
                        $data = array_merge($data, $insertDataArray);
                    } else {
                        $insertData = $value->getDataForResource($this->model->$getter(), $object);
                        $data[$key] = $insertData;
                    }
                } else if (method_exists($value, "save")) {
                    // for fieldtypes which have their own save algorithm eg. fieldcollections
                    $value->save($this->model);
                }
            }
        }

        $this->db->insertOrUpdate($storetable, $data);


        // get data for query table 
        // $tableName = $this->model->getDefinition()->getTableName($object->getClass(), true);
        // this is special because we have to call each getter to get the inherited values from a possible parent object


        Object_Abstract::setGetInheritedValues(true);

        $objectVars = get_object_vars($this->model);

        $data = array();
        $data["o_id"] = $object->getId();
        $data["fieldname"] = $this->model->getFieldname();
        $this->inheritanceHelper->resetFieldsToCheck();
        $oldData = $this->db->fetchRow("SELECT * FROM " . $querytable . " WHERE o_id = ?", $object->getId());

        foreach ($objectVars as $key => $value) {
            $fd = $this->model->getDefinition()->getFieldDefinition($key);

            if ($fd) {
                if ($fd->getQueryColumnType()) {
                    //exclude untouchables if value is not an array - this means data has not been loaded

                    $method = "get" . $key;
                    $insertData = $fd->getDataForQueryResource($this->model->$method(), $object);

                    if (is_array($insertData)) {
                        $data = array_merge($data, $insertData);
                    }
                    else {
                        $data[$key] = $insertData;
                    }


                    //get changed fields for inheritance
                    if($fd->isRelationType()) {
                        if (is_array($insertData)) {
                            $doInsert = false;
                            foreach($insertData as $insertDataKey => $insertDataValue) {
                                if($oldData[$insertDataKey] != $insertDataValue) {
                                    $doInsert = true;
                                }
                            }

                            if($doInsert) {
                                $this->inheritanceHelper->addRelationToCheck($key, array_keys($insertData));
                            }
                        } else {
                            if($oldData[$key] != $insertData) {
                                $this->inheritanceHelper->addRelationToCheck($key);
                            }
                        }

                    } else {
                        if (is_array($insertData)) {
                            foreach($insertData as $insertDataKey => $insertDataValue) {
                                if($oldData[$insertDataKey] != $insertDataValue) {
                                    $this->inheritanceHelper->addFieldToCheck($insertDataKey);
                                }
                            }
                        } else {
                            if($oldData[$key] != $insertData) {
                                $this->inheritanceHelper->addFieldToCheck($key);
                            }
                        }
                    }

                }
            }
        }

        $this->db->insertOrUpdate($querytable, $data);

        $this->inheritanceHelper->doUpdate($object->getId());
        $this->inheritanceHelper->resetFieldsToCheck();

        // HACK: see a few lines above!
        Object_Abstract::setGetInheritedValues($inheritedValues);

    }

{'fraction_non_alphanumeric': 0.13124954778959555, 'fraction_numerical': 0.0011576586354098836, 'mean_word_length': 1.6478927203065135, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 188, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2975028', 'n_tokens_mistral': 3609, 'n_tokens_neox': 3451, 'n_words': 732}