우리 애플리케이션의 매핑 구조를 작업하는 동안 코드 일관성 문제가 발생했습니다. Zend_Db_Select 클래스 ($ select-> from ('table') -> where ('id = ?, 1)와 같은 함수를 사용하여 선택 쿼리를 만드는 것은 쉬운 일이지만 업데이트/삭제 쿼리에는 작동하지 않습니다. select를 빌드하는 것처럼 Zend_Db_Update 나 Zend_Db_Delete와 같은 클래스가 아니므로 아래 코드에서 볼 수있는 것처럼 Zend_Db_Select 클래스를 확장했습니다.이 코드는 Zend_Db_Select 클래스를 확장하는 사용자 정의 클래스를 보여줍니다 이 기본되지 않는 이유는Zend_Db_Select 업데이트/삭제 쿼리의 경우
<?php
class Unica_Model_Statement extends Zend_Db_Select
{
public function __construct($oMapper)
{
parent::__construct($oMapper->getAdapter());
$this->from($oMapper->getTableName());
}
/**
* @desc Make a string that can be used for updates and delete
* From the string "SELECT `column` FROM `tabelnaam` WHERE `id` = 1" only the part "`id = `" is returned.
* @return string
*/
public function toAltString()
{
$sQuery = $this->assemble(); // Get the full query string
$sFrom = $this->_renderFrom(''); // Get the FROM part of the string
// Get the text after the FROM (and therefore not using the "SELECT `colname`" part)
$sString = strstr($sQuery,$sFrom);
// Delete the FROM itself from the query (therefore deleting the "FROM `tabelnaam`" part)
$sString = str_replace($sFrom, '', $sString);
// Delete the word "WHERE" from the string.
$sString = str_replace('WHERE', '', $sString);
return $sString;
}
}
################################################
# Below code is just to demonstrate the usage. #
################################################
class Default_IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$person = new Unica_Model_Person_Entity();
$statement = new Unica_Model_Statement($person->getMapper());
$statement->where('id = ?' , 1);
$person->getMapper()->delete($statement);
}
}
class Unica_Model_Person_Mapper
{
public function delete($statement)
{
$where = $statement->toAltString();
$this->getAdapter()->delete($this->_tableName,$where);
}
}
모두가이 클래스를 사용하여 잘 작동하지만 사용되는 방법을 보여 하단에 최소한의 예제 코드와 함께. 우리가 어쩌면 뭔가가 있다면 우리가 궁금해. 가지고 있습니까 갱신/선택을위한 클래스를 삭제하고이 클래스를 사용하면 다른 곳에서 문제가 발생합니까?
조언을 주시면 감사하겠습니다. 사전에 감사합니다,
Ilians
감사합니다, 당신은 오른쪽 따옴표에 대해 위치 : 예를 들어, 여러 "AND로"절에 대한 가능한 솔루션은 다음이 될 수 있습니다. 나는 프로그래머가 모든 것을 인용하는 것을 귀찮게하지 않아도되도록 자동으로 모든 것을 인용하려고 노력하고있다. (그가 잊어 버렸다하더라도 그것은 안전하다.) 그러나 myDelete 함수는 어디에서 여러 값으로 작동합니까? Like : "id = 3 OR adress = 'test'AND 언어! = 'EN' – Ilians
WHERE 절이 얼마나 복잡한 지에 따라 코드를 어느 정도 수정해야합니다. – dinopmi
나는 비슷한 접근법을 시도했지만 유사한 배열 키의 문제에 봉착했다. 상황 : 배열 ( 'id'=> 3, 'ID'=> 4 ) 는 posibilities 어레이 ( 보다 활성화하기 위해 비록처럼 사용 "ID =? => 3 'ID =? => 4 ) – Ilians