문제

나는 SimpleTest를 통해 CakePHP 1.3에서 단위 테스트를 작성하고 있습니다.

나는 작동하는 다음 코드를 실행합니다 (예상대로 작성)

$this->testAction('/post/add/user:'. $userId, array('method' => 'post'));

다음 두 테스트에서 사용하기 위해 추가 된 게시물의 ID를 찾아야합니다. 게시물을 편집하고 게시물을 삭제합니다.나는 (즉, $ POST-> ID, $ this-> post-> ID) 모델을 통해 참조를 시도했지만, 증거를 통과하는 것이 동일한 방식으로 작동하지 않는 것처럼 보입니다.

또한 imax (ID)를 찾으려고 쿼리 할 수 있지만, 거의 에 새로운 게시물을 삽입하는 테스트 시스템에서 다른 사용자가있는 경우,결과를 망칠 수 있습니다 (적어도 내가 아는 한 - 내가 틀렸다면 나를 바로 잡으십시오).

도움이 되었습니까?

해결책

Usually, from a controller you should use

$this->Post->getLastInsertID();

This is a wrapper for connectors methods.

$this->Post->id 

is used to set an id and then retrieve or set data.

다른 팁

I never used cakephp before, but with mysql and mysqli you can get the last inserted ID with the following:

mysql_insert_id(), and $mysqli->insert_id respectively.

http://php.net/manual/en/function.mysql-insert-id.php

http://php.net/manual/en/mysqli.insert-id.php

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top