When adding $select->order(...) to a previously combined Select in Zend Framework 2 the "order by" will only be added to the first Select statement.
SELECT `user`.*
FROM `user` WHERE `user_id` = :where1
ORDER BY `starttime_dt` ASC )
UNION
( SELECT `user`.*
FROM `user`
WHERE `user_id` != :subselect1where1 )
What I need is that the sorting will be applied to the full Select. A solution is already described here
https://github.com/zendframework/zf2/issues/5162#issuecomment-36294281
But this solution works with an SQL-Adapter.
How can I do that with a Table Gateway?
My code looks currently like this (resulting to the above statement)
$select = $this->tableGateway->getSql()->select();
$select2 = clone $select;
$select->where->equalTo('user_id', $user_id);
$select2->where->notEqualTo('user_id', $user_id);
$select->combine($select2);
$select->order('starttime_dt');
$resultSet = $this->tableGateway->selectWith($select);
You would do it with table gateway the same way it was illustrated in the link you provided.
Your code would look like
$select = $this->tableGateway->getSql()->select();
$select2 = clone $select;
$select->where->equalTo('user_id', $user_id);
$select2->where->notEqualTo('user_id', $user_id);
$select->combine($select2);
$combinedSelect = (new Zend\Db\Sql\Select)->from(['sub' => $select])->order('starttime_dt');
$statement = $this->sql->prepareStatementForSqlObject($combinedSelect);
$result = $statement->execute();
$resultSet = clone $this->resultSetPrototype;
$resultSet->initialize($result);
You wouldn't be able to use the TableGateway's selectWith method because it's going to check that the provided Select's table matches the TableGateway's table.
Related
I use laravel 5.2 I'm trying to get latest intervention of each foreing key pmt_id like bellow:
$res = $this->model->with('typo', 'nro', 'ctr', 'cad', 'pm')
->join('a2t_intervention',function ($join) use ($salar){
$join->on('a2t_intervention.pmt_id','=','a2t_pmt.id');
$join->whereRaw('a2t_intervention.pmt_id = (select max(`pmt_id`) from a2t_intervention)');
$join->where('a2t_intervention.etat_intervention','like','nok');
$join->whereIN('a2t_intervention.id_equipe_stt',$salar);
});
but I get this ERROR :
Call to undefined method Illuminate\Database\Query\JoinClause::whereRaw()
I try other ways but nothig work for me.
for each pmt_id in the table intervention we have at least one record ,I'am looking for get the last intervention foreach single pmt_id before make join with table PMT.
how to select id from table intervention in latest pmt_id like bellow in sql query:
SELECT t.*
FROM ( SELECT pmt_id
, MAX(id) AS id
FROM a2t_intervention
WHERE etat_intervention = 'nok'
AND `id_equipe_stt` IN ('" . implode(',', $id_equipe_stt) . "')
GROUP
BY pmt_id ) AS m
INNER JOIN a2t_intervention AS t
ON t.pmt_id = m.pmt_id
AND t.id = m.id
Error is pretty self-explanatory - there is no whereRaw method. You could try to replace:
$join->whereRaw('a2t_intervention.pmt_id = (select max(`pmt_id`) from a2t_intervention)');
with
$join->where('a2t_intervention.pmt_id', '=', \DB::raw("(select max(`pmt_id`) from a2t_intervention)"));
In Later Laravel versions JoinClause extends Builder so whereRaw method is available but for Laravel 5.2 it isn't.
i've got a few tables in MDS.
One table (clients) ist filled via SQL and the other one is a masterdata table (country) filled by hand.
I have a business rule on table clients:
"Name must be unique" and no b-rule on Country.
I want to validate the data PROGRAMMATICALLY i do not want to CLICK "apply business rules" in explorer window on the webinterface.
I found several threads about how to use the sp mentioned in the title (udpValidateModel) to validate all entities in a model.
well...this thing does nothing. I can see the validationStatus in each of my tables "Awaiting Revalidation" after changing business rules or update data via sql. It doesnt matter what i do the status wont change (neither the validation icons in webui).
i also tried validateentity but the same "nothing" happens.
The SP below:
DECLARE #User_ID int
DECLARE #Model_ID int
DECLARE #Version_ID int
SET #User_ID = (SELECT ID FROM [MasterDataServices].[mdm].[tblUser] where userName = SYSTEM_USER )
SET #Model_ID = (SELECT Top 1 Model_Id FROM [MasterDataServices].[mdm].[viw_SYSTEM_SCHEMA_VERSION]
WHERE Model_MUID = 'MYMODELID')
SET #Version_ID = (SELECT Top 1 VersionNbr FROM [MasterDataServices].[mdm].[viw_SYSTEM_SCHEMA_VERSION]
WHERE Model_MUID = 'MYMODELID'
ORDER BY ID DESC )
EXECUTE [MasterDataServices].[mdm].[udpValidateModel] #User_ID, #Model_ID, #Version_ID, 1
Can anyone help?
SP 'udpValidateModel' works perfectly fine, looks like the parameters you are populating is not correct.
You may correct this as below and try; make sure that the system user has full authorization for the model.
SET #User_ID = (SELECT ID FROM [MasterDataServices].[mdm].[tblUser] where userName = SYSTEM_USER )
SET #Model_ID = (SELECT Top 1 Model_Id FROM [MasterDataServices].[mdm].[viw_SYSTEM_SCHEMA_VERSION]
WHERE Model_MUID = 'MYMODELID')
SET #Version_ID = (SELECT Top 1 VersionNbr FROM [MasterDataServices].[mdm].[viw_SYSTEM_SCHEMA_VERSION]
WHERE Model_MUID = #Model_ID
ORDER BY ID DESC )
I have two relation table "demande" and "reponse" which are linked and I'd like to retrieve all the lines of "demande" not in "reponse".
I try this with a join :
$select = new Select ();
$select->columns(array("id"));
$select->from ("demande" );
$select->where->lessThan("dateArretMarche",$stringDate );
$select->join(
array("rep" => 'reponse'), // table name,
'demande.id = rep.id_demande',array(),
$select::JOIN_RIGHT);
$select ->where->isNull("rep.id");//<== it doesn't work
But I can't select "null" lines".
I guess it's possible with "not in" but Zend Framework provide only "in" predicate.
Thank all.
What if you just include it in the query? Like this:
$select ->where('rep.id IS NULL');
If you want to use isNull condition.So do you need to use with Predicate.Try with following code.
$select->where(array(
new \Zend\Db\Sql\Predicate\IsNotNull("rep.id")
)
);
I need last insert id after executing a insert statement. Now I am not using TableGateway so $this->lastInsertValue is not available to me. What other options are available if I need to use Insert statement through Sql Object not a Table Gateway Object.
$objInsert = new Insert('name_master');
$objInsert->values(array( 'username' => $name,
'price' => 0,
'is_approval_needed' => 'n'
));
$sql = new Sql($this->adapter);
$result = $sql->prepareStatementForSqlObject($objInsert)->execute()->getAffectedRows();
As I need to execute multiple insert statements in different tables using last insert id of previous insert, Now I want to do it in a single method of my Model.
The Zend\Db\Adapter\Driver\DriverInterface specifies a getLastGeneratedValue() method, so presumably this should work...
$lastId = $this->adapter->getDriver()->getLastGeneratedValue();
$dbAdapter = $this->tableGateway->adapter;
$lastId = $dbAdapter->getDriver()->getConnection()->getLastGeneratedValue();
Right now the code below looks for the author_id and the target_object_id in the database and requires both to match, to be retreived by the query. I would like it to be WHERE author_id='x' || target_object_id='y' instead of it being && by default, which is what the code below does.
I've looked on Propel and the Symfony manuals, and they don't help me.
$c = new Criteria();
$c->add("type", GVCCommentPeer::TYPE_PRIVATE_MESSAGE);
$c->add("author_id", $this->myprofileid);
$c->add("target_object_id", $this->profile->getId());
You need to create Criterion objects from your Criteria object and merge them on the condition you require:
//create your Criteria
$c = new Criteria();
$c->add("type", GVCCommentPeer::TYPE_PRIVATE_MESSAGE);
//create 2 Criterion objects
$c1 = $c->getNewCriterion("author_id", $this->myprofileid);
$c2 = $c->getNewCriterion("target_object_id", $this->profile->getId());
// merge the two fields on OR
$c1->addOr($c2);
//bind
$c->add($c1);
$result = YOURPEERHEREPeer::doSelect($c);