Twig & PHP ActiveRecord - 가입 테이블에서 필드에 액세스 할 수 없음
-
13-12-2019 - |
문제
phpactiveRecord / twig와 함께 가입 된 테이블에 액세스 할 수 없습니다. 여기에 단순화 된 코드가 있습니다. 두 모델 (코드 및 사용자)이 있으며 각 코드는 한 사용자에 속해 있으므로 코드의 이름과 코드를 나열하고 싶습니다.
php
// model
class Code extends ActiveRecord\Model {
static $belongs_to = array(
array('user'),
);
}
class User extends ActiveRecord\Model {
static $has_many = array(
array('code'),
);
}
// controller
$codes = Code::all(array('include' => 'user'));
var_dump($codes); //-> successfully displayed codes list and their authors
$this->twig->display('codelist.twig', $codes);
.
template.twig
{% for code in codes %}
{{ code.name }} //-> successfully displayed code's name
{{ code.user.name }} //-> failed to output user's name with error
{% endfor %}
// error:
// An exception has been thrown during the rendering of a template ("Call to undefined method: user") in "inc/template.twig" at line **.
.
이 페이지를 보았습니다. http://twig.sensiolabs.org/doc/templates.html
구현
편의를 위해 foo.bar는 다음과 같은 PHP에 다음과 같습니다. 레이어 :
foo가 배열 및 막대가 유효한 요소인지 확인하십시오. 그렇지 않은 경우, 그리고 foo. 객체인지, 막대가 유효한 속성인지 확인합니다. 그렇지 않은 경우, 그리고 foo. 객체인지, 바가 유효한 방법인지 확인하십시오 (심지어 생성자 - __construct ()를 대신 사용); 그렇지 않은 경우, foo가 an. 객체, GetBar가 유효한 방법인지 확인하십시오. 그렇지 않은 경우, foo가 an. 객체, ISBAR이 유효한 메소드인지 확인합니다. 그렇지 않은 경우, 널을 반환하십시오 값. 다른 한편으로는 Foo [ 'bar']는 PHP 배열에서만 작동합니다.
foo가 배열 및 막대가 유효한 요소인지 확인하십시오. 그렇지 않은 경우, A. null 값.
$ 코드 [0] -> 사용자를 통해 사용자 속성에 액세스 할 수 있지만, 나뭇 가지 템플릿 파일에서 사용자 속성에 액세스 할 수없는 이유는 무엇입니까?
해결책
송아지 덕분에 문제를 해결했습니다.phpactiveRecord의 lib / model.php에서 함수 __isset을 교체했습니다.
/**
* Determines if an attribute exists for this {@link Model}.
*
* @param string $attribute_name
* @return boolean
*/
public function __isset($name)
{
// check for getter
if (method_exists($this, "get_$name"))
{
$name = "get_$name";
$value = $this->$name();
return $value;
}
return $this->read_attribute($name);
}
.