2

大家好,我正在使用zend框架(但我认为这无关紧要)和php5,我只想修改一个对象的对象

  public function saveSite($post) {
    $form = new Diff_Form_Download();
    $subform = new Diff_Form_DownloadSubform();
    $form = $this->view->form;
    $newSite = 0;
    $sitesRecord = new Diff_Model_Sites();
    $debugString = null;

    if (is_array($post)) {
        $subform = $this->getSubformByPost($post);
        $debugString = $subform->getContent();
        echo $debugString;

        //new site instertion
        if (is_null($subform)) {
            $subform = $form->getSubForm('NewSite');
            $newSite = 1;
        }

        $values = reset($subform->getValues());

       $sitesRecord = Doctrine_Core::getTable('Diff_Model_Sites')->findOneBy('idSite', $values['idSite']);
        if ($sitesRecord) {
            $sitesRecord->idSite = $values['idSite']; //useless but necessary to make Doctrine understand to use update?
        } else { //if the record is not present instantiate a new object (otherwise save() will not work
            $sitesRecord = new Diff_Model_Sites();
        }

        $sitesRecord->content = $subform->getContent(); //$values['content'];
        $sitesRecord->newHtml = $subform->getContent();
        $sitesRecord->url = $values['url'];
        $sitesRecord->shortName = $values['shortName'];
        $sitesRecord->lastChanged = date("Y-m-d H:i:s");
        $sitesRecord->pollingHours = $values['pollingHours'];
        $sitesRecord->minLengthChange = $values['minLenghtChange'];

        if (Zend_Auth::getInstance()->hasIdentity()) { //save the owner
            $sitesRecord->idOwner = Zend_Auth::getInstance()->getIdentity()->idOwner; //retrieve the owner
            $sitesRecord->save();
        } else {
            throw new Exception("your session have expired: \n please login again");
        }
    }
}

/**
 * return the calling subform
 * @param type $post
 * @return type 
 */
public function getSubformByPost($post) {
    $form = new Diff_Form_Download();
    $form = $this->view->form;
    $subform = new Diff_Form_DownloadSubform();
    $subformName = "site" . $post['idSite'];
    $subform = $form->getSubForm($subformName);

    return $subform;
}

public function refreshOneDiff($post) {
     $debugString;
    if (is_array($post)) {
        $form = new Diff_Form_Download();
        $form = $this->view->form;

        $subform = new Diff_Form_DownloadSubform();
        $subform = $this->getSubformByPost($post);

        if (!$subform)
            $subform = $this->view->form->getSubformByPost('NewSite');
        $url = $subform->getUrl();
        $idSite = $subform->getIdSite();

        $crawler = new Crawler();
        $newpageContent = $crawler->get_web_page($url);
        $siteRecord = new Diff_Model_Sites();
        $siteRecord = $subform->getSiteRecord();
        if ($siteRecord) //check if the record is not null
            $oldpageContent = $siteRecord->get('content');
        else
            $oldpageContent = null;

        $differences = $this->getDiff($oldpageContent, $newpageContent);

        if (!is_null($differences)) {
            $siteRecord->content = $newpageContent;
            $subform->setContent($newpageContent);
            $subform->setContentDiff($differences);
            $subform->setSiteRecord($siteRecord);
            $subform = $this->getSubformByPost($post);
            $debugString = $subform->getContent();
        }

            //echo $debugString;

        $sitePresence = Doctrine_Core::getTable('Diff_Model_Sites')->findOneBy('idSite', $idSite);
        if ($sitePresence) {
            //$this->saveSite($post);
            $this->debugtry($post);
        }
    } else {

    }
}

基本上我在这里做的是:1)从视图中获取表单 2)从表单中获取子表单(我们称之为 SubformX) 3)从 SubformX 中获取对象“siteRecordX” 4)更改“siteRecordX”的值" 5) 调用函数 saveRecord() 6) 在此函数中重新获取相同的 SubformX 并回显对象 siteRecordX 的值

令人惊讶的是,如果我更改 SubformX 的变量,它将保持这种状态,如果我更改 SubformX 的对象的变量,它将保持不变(如果我检索 SubformX)。看起来,即使 SubformX 是通过引用传递的,它的子对象也不相同,它是按值传递的,因此它们会随着函数的上下文而消失。你能帮我么?谢谢

编辑我仍然无法解决这个问题,也无法理解:这是子表单的构造函数:

    public function __construct($site = null, $options = null) {
    if ($site instanceof Diff_Model_Sites) {
        $this->_shortName = $site->get('shortName');
        $this->_url = $site->get('url');
        $this->_content = $site->get('content');
        $this->_idSite = $site->get('idSite');
        $this->_siteRecord = $site;
        $this->_lastChanged = $site->get('lastChanged');
    }parent::__construct($options);}

而这是我用来设置值的 SubformX 的功能。

public function setContent($contentText) {
    $this->_siteRecord->content = $contentText;
    $this->_content = $contentText;
}

这是我调用以获取值的子表单的功能

    public function getContent() {
    if (isset($this->_siteRecord)) {
        //return $this->_content;
        return $this->_siteRecord->content;
    }
}

通过注释行,我能够检索更新的值,而不是第二个。这对我来说是一个真正的谜,因为我在完全相同的点以完全相同的方式设置和获取它们,我无法理解其中的区别。

4

2 回答 2

0

您的_siteRecord属性是一个 ORM 对象(Doctrine,它出现了)。所以它的数据可能有一些不符合引用类型对象标准的行为。__get它肯定对and有一些覆盖__set。它还使用缓存。这些东西是保持数据库模型交互正常工作所必需的,但它们可能会混淆应该是引用类型和值类型。(见: http: //www.codinghorror.com/blog/2006/06/object-relational-mapping-is-the-vietnam-of-computer-science.html

PS:在您的构造函数中,您使用:

$this->_content = $site->get('content');
$this->_siteRecord = $site;

但是在您的 getContent() 中,您使用:

$this->_siteRecord->content;

这种差异可能是问题的一部分。

于 2012-08-07T22:45:02.530 回答
0

Thank you all guys. It was Doctrine caching. I have not investigated further the problem, but after getting rid of Doctrine everything works fine. I have lost one entire day after this issue. Moreover today I have lost another day for another curious problem related to Doctrine. It seems that each time you gather data from your db Doctrine decode them for you (just like the php function utf8_decode($data). Of course if you get the data and then put it back in the db again it will result in a total mayhem of coding and decoding. This is enough. I still think that ORM are great programming tools but simply Doctrine is not. I will not rest in peace since I'll have it eliminated from my program. I will use Zend_Db library instead. Which I have mostly already done without regret and without facing the above-mentioned Doctrine's problems. Again thanks to Seth Battin and Dave Random for the help and patience to understand my noob-coding.

于 2012-08-09T16:00:02.937 回答