2012-08-07 9 views
2

안녕 난 (하지만이 부적절하다고 생각)와 PHP5 난 그냥 내가 여기서 뭘하는지 개체 기본적으로PHP 객체 전달 객체 : 참조 또는 값?

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) 서브폼에서 "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; 
    } 
} 

를 얻기 위해 전화를 하위 폼의 기능이다. 이것은 정확하게 동일한 지점에서 정확하게 동일한 방식으로 설정하고 차이를 이해할 수 없기 때문에 나에게 정말 신비입니다.

+1

http://php.net/manual/en/language.oop5.references.php - 그 정보가 문제를 더욱 흐리게 할 수 있음을 인정하지만. 짧게는 * 모든 * 객체는 실제로 객체를 전달하지 않고 컨텍스트에 관계없이 항상 참조로 전달되지만 포인터는 객체를 전달합니다. 즉, 포인터 복사본을 만들더라도 여전히 동일한 것을 가리 킵니다. PHP5에서 객체의 복사본을 만드는 유일한 방법은'clone'입니다. 코드의 나중에 수정되지 않은 것으로 보이는 개체는 수정하지 않았기 때문입니다. – DaveRandom

+0

귀하의 의견을 보내 주셔서 감사합니다. 모든 indexAction은 각 하위 폼의 생성자를 다시 호출하며, 하위 폼을 다시로드하면 저장되기 전에 db에서 요소를 검색합니다. 나는 아직도 어떻게이 행동을 바꾸는 지 알아 내야 만한다. 어떤 도움을 주셔서 감사합니다. 어디 shoud 내가 데이터베이스에서 그들의 가치를로드, 개체를 인스턴스? 감사 – user1439509

답변

0

_siteRecord 속성은 ORM 개체입니다 (Doctrine, 나타납니다). 따라서 해당 데이터에는 참조 유형 객체에 대해 표준이 아닌 일부 동작이있을 수 있습니다. 확실히 __get__set의 무시가 있습니다. 또한 캐싱을 사용합니다. 이러한 것들은 데이터베이스 모델 상호 작용을 제대로 유지하는 데 필요하지만 참조 및 값 유형이어야하는 것을 혼동시킬 수 있습니다. (참조 : http://www.codinghorror.com/blog/2006/06/object-relational-mapping-is-the-vietnam-of-computer-science.html를)

P.S을 : 당신의 생성자에서 당신이 사용

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

그러나 당신의 getContent에

()는 당신이 사용

$this->_siteRecord->content; 

그 차이는 문제의 일부가 될 수 있습니다.

0

감사합니다. Doctrine 캐싱이었습니다. 나는 문제를 더 깊이 조사하지는 않았지만 Doctrine을 제거한 후에는 모든 것이 잘 작동한다. 이번 호 이후로 하루 종일 길을 잃었습니다. 또한 오늘 나는 교리와 관련된 또 다른 호기심 많은 문제에 대한 또 다른 날을 잃었습니다. db Doctrine에서 데이터를 수집 할 때마다 (php 함수 인 utf8_decode ($ data)처럼) 데이터를 디코딩해야합니다. 물론 데이터를 가져 와서 다시 db에 넣으면 코딩 및 디코딩의 총 마비. 충분합니다. 나는 여전히 ORM이 훌륭한 프로그래밍 도구라고 생각하지만 간단하게 Doctrine은 아니다. 내 프로그램에서 제거 할 것이므로 안심할 것입니다. 대신 Zend_Db 라이브러리를 사용할 것입니다. 나는 대부분 후회없이 앞서 언급 한 교리 문제에 직면하지 않고 이미 해왔습니다. Seth Battin과 Dave Random에게 도움을 주신 데 대해 다시 한번 감사드립니다.