Propel: what is the equivalent of OR in SQL? - criteria

what is the method to create OR?
I mean: I know to craate this SQL clause:
SELECT * FROM author WHERE author.FIRST_NAME = 'Karl' AND author.LAST_NAME <> 'Marx';
I should do this:
<?php
$c = new Criteria();
$c->add(AuthorPeer::FIRST_NAME, "Karl");
$c->add(AuthorPeer::LAST_NAME, "Marx", Criteria::NOT_EQUAL);
$authors = AuthorPeer::doSelect($c);
But if i want to create:
SELECT * FROM author WHERE author.FIRST_NAME = 'Karl' OR author.LAST_NAME <> 'Marx';
what should i do?
Regards
Javi

$c = new Criteria();
$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Karl");
$cton2 = $c->getNewCriterion(AuthorPeer::LAST_NAME, "Marx", Criteria::NOT_EQUAL);
$cton1->addOr($cton2);
$c->add($cton1);

Check orWhere(), combine(), and the new _or() operator:
http://www.propelorm.org/wiki/Documentation/1.5/ModelCriteria#CombiningSeveralConditions
http://propel.posterous.com/using-or-in-propel-queries-becomes-much-easie

Related

Converting mysql_query to mysqli_query

I just moved to xampp version 7.0.13, which now has PHP 7.x, and the following query no longer works. I was Hoping someone can help me convert the following to mysqli query. I don't write code, so any help you could give me would be great.
// Count the number of rows in the member table
$rows = "SELECT * FROM $db_name.$tbl_name";
$count_rows = mysql_query( $rows );
here is your example with PDO:
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
$sql = 'SELECT * FROM :table';
$statement = $pdo->prepare($sql);
$count = $statement->execute(array('table' => 'your-table-name'))->rowCount();

SugarCrm find Opportunities by account name and name

I'v done this query to look for opportunities based on account name and name:
public function getOpportunity($session_id, $client, $email, $name){
$acc = $this->getAccount($session_id, $client, $email);
$acc_id = $acc->entry_list[0]->id;
$query2 = "
opportunities.id in
(select accounts_opportunities.opportunity_id from
accounts_opportunities inner join
accounts where
accounts_opportunities.account_id = accounts.id and
accounts_opportunities.deleted = 0 and
accounts.deleted = 0 and
accounts.id = '{$acc_id}')
and opportunities.deleted=0
and opportunities.name = '{$name}'";
$op = $client->get_entry_list($session_id, 'Opportunities', $query2);
return $op;
}
For some reason, it is returning me an error with erro 40 access denied. I don't know why this query is not working. I did a similar one, to find Accounts by email, and it successfully return me the results i want without any error. That query is the following:
public function getAccount($session_id, $client, $email){
$query = "accounts.id in
( select bean_id from
email_addr_bean_rel inner join
email_addresses where
email_addr_bean_rel.email_address_id = email_addresses.id
and email_addr_bean_rel.deleted = 0 and
email_addresses.deleted = 0 and
bean_module = 'Accounts' and
email_addresses.email_address = '{$email}' )";
$acc = $client->get_entry_list($session_id, 'Accounts', $query);
return $acc;
}
Any help would be appreciated. Thank you.
EDITED: I'm using SOAP API and PHP.
See this blog post for more details...
http://developers.sugarcrm.com/wordpress/2012/03/19/howto-avoiding-subqueries-with-our-web-services/

How do I do this sql query in Zend?

How do I do this sql query in Zend Framework, I need to some how do this in the PDO context I think? I tried ->query but not sure if I am getting this right. The three variables are user_id and to and from date.
SELECT
ss.subcategory_id,
ss.subcategory_name,
ss.subcategory_issaving,
IFNULL(SUM(m.mv_monthly_total),0) AS expendsum
FROM
(SELECT
s.subcategory_id,
s.subcategory_name,
s.subcategory_issaving
FROM
subcategory s
WHERE
s.subcategory_isexpend = 'Y'
AND
s.subcategory_issaving = 'Y') ss
LEFT JOIN
mv_monthly m
ON ss.subcategory_id = m.mv_monthly_subcategory_id
AND m.mv_monthly_user_id = 2
AND m.mv_monthly_month >= '2010-01-01'
AND m.mv_monthly_month <= '2020-01-01'
GROUP BY
ss.subcategory_id,
ss.subcategory_name,
ss.subcategory_issaving
ORDER BY
ss.subcategory_issaving DESC,
expendsum;
I have tried the following with no luck
$db = Zend_Db_Table::getDefaultAdapter();
$dbExpr1 = new Zend_Db_Expr("s.subcategory_id, s.subcategory_name, s.subcategory_issaving");
$dbExpr2 = new Zend_Db_Expr("ss.subcategory_id, ss.subcategory_name, ss.subcategory_issaving, IFNULL(SUM(m.mv_monthly_total),0) AS expendsum");
$select = $db->select()
->from(
array(
'ss' => new Zend_Db_Expr(
'('. $db->select()
->from(array("s" => "subcategory"), $dbExpr1)
->where("s.subcategory_isexpend = 'Y'")
->where("s.subcategory_issaving = 'Y'") .')'
)
),
$dbExpr2
)
->joinLeft(array("m" => "mv_monthly"), "ss.subcategory_id = m.mv_monthly_subcategory_id")
->where("m.mv_monthly_user_id = ?", $user_id)
->where("m.mv_monthly_month >= ?", $fromMonth)
->where("m.mv_monthly_month <= ?", $toMonth)
->group(array("ss.subcategory_id","ss.subcategory_name","ss.subcategory_issaving"))
->order(array("ss.subcategory_issaving DESC", "expendsum"));
$row = $db->fetchAll($select);
For such a complex query, you can just execute it directly rather than using the object oriented approach as it gets fairly complicated with a query like that.
Try something like this, replacing my query with yours, and binding your variables into the query:
$db = Zend_Db_Table::getDefaultAdapter();
$stmt = new Zend_Db_Statement_Pdo($db, 'SELECT a, b, c FROM a WHERE username = ? AND date = ?');
try {
$res = $stmt->execute(array($user_id, $fromMonth));
if ($res) {
$rows = $stmt->fetchAll();
}
} catch (Zend_Db_Statement_Exception $dbex) {
// log Query failed with exception $dbex->getMessage();
}
If you prefer to use the object oriented approach, or need to because some parts of the query will be conditional, I usually build by subqueries up first as their own select, and you can simply embed those in to the main query with the select object for the subquery.
Here is what I mean by that:
$subselect = $this->getDbTable()
->select()
->from('mytable', array('time' => 'max(time)', 'id'))
->where('id IN (?)', $serialNumbers)
->group('id');
$select = $this->getDbTable()
->select()
->setIntegrityCheck(false)
->from('mytable')
->join('other', 'mytable.id = other.id', array('label'))
->join(array('dt' => $subselect),
'(mytable.time, mytable.id) = (dt.time, dt.id)', '');

How do left outer joins work on Zend framework

I have this SQL query:
SELECT pais FROM pais LEFT OUTER JOIN users_has_pais ON pais.id = users_has_pais.pais_id WHERE users_has_pais.users_id = 100
And I'm trying to write something similar within a model using the leftJoin method from Zend_Db_Table but I have no clue on what I'm doing.... I tried with something like this:
$resultSetPais = Zend_Db_Table::getDefaultAdapter();
$some = $resultSetPais->select()
->joinLeft( array ( 'users_has_pais' => 'users' ),
'pais.id = users_has_pais.pais_id', 'pais' );
But truth is I have no idea how to make it work and this code just returns the adapter information.
SOLVED:
$instance = Zend_Db_Table_Abstract::getDefaultAdapter();
$pais = $instance->select();
$pais->from(array('p' => 'pais'), array('p.pais') )
->join( 'users_has_pais', 'p.id = users_has_pais.pais_id' )
->where( 'users_has_pais.users_id = ?', $row->id );
$paisEntry = $instance->fetchCol($pais);
I'm adding the answer to the question as suggested by #Jaitsu. For this kind of left join:
SELECT pais FROM pais LEFT OUTER JOIN users_has_pais ON pais.id = users_has_pais.pais_id WHERE users_has_pais.users_id = 100
The code should be something like:
$instance = Zend_Db_Table_Abstract::getDefaultAdapter();
$pais = $instance->select();
$pais->from(array('p' => 'pais'), array('p.pais') )
->join( 'users_has_pais', 'p.id = users_has_pais.pais_id' )
->where( 'users_has_pais.users_id = ?', $row->id );
$paisEntry = $instance->fetchCol($pais);
hey this code produces an INNER JOIN sql query, not an OUTER JOIN - it is different thing, right? so, what should be the correct way of doing OUTER JOIN?

Translating a query to use Zend_Db_Select

I'm having some problems translating this query to use ZF's Zend_Db_Select:
SELECT b.id, b.title, b.description
FROM memberships AS m
JOIN blogs AS b ON b.id = m.blog_id
WHERE m.user_id = ?
ORDER BY m.created
LIMIT 0, 30
(this query works and returns results)
Memberships is a link table between blogs and users. It's a simple | id | blog_id | user_id | affair.
Here's what I have so far:
// $table = Zend_Db_Table instance, $id = a user id
$select = $table->select()
->from(array('m' => 'memberships'), array('b.id', 'b.title', 'b.description'))
->join(array('b' => 'blogs'), 'b.id = m.blog_id')
->where('m.user_id = ?', (int) $id)
->order('m.created DESC')
->limit(0, 30);
This is the (strange (to me)) error I'm getting:
#0: Select query cannot join with another table
Occurred on line 211 of D:\...\library\Zend\Db\Table\Select.php.
Thanks for your help.
You could also still use the traditional $model->select() object by adding setIntegrityCheck(false), like so.
$select = $table->select()
->setIntegrityCheck(false)
->from(array('m' => 'memberships'), array('b.id', 'b.title', 'b.description'))
->join(array('b' => 'blogs'), 'b.id = m.blog_id')
->where('m.user_id = ?', (int) $id)
->order('m.created DESC')
->limit(0, 30);
This disables the check that is throwing the exception:
#0: Select query cannot join with another table
When retrieved from your table object, the statement will be limited to that table I think. The Zend_Db_Table::select() methods returns a Zend_Db_Table_Select object which is a subclass of Zend_Db_Select and imposes this restriction. Try this instead:
$db = Zend_Db::factory( ...options... );
$select = new Zend_Db_Select($adapter);
$select->from( 'my_table_name' )->join( ...
If you prefer, the following should be equivalent:
$db = Zend_Db::factory( ...options... );
$db->select()->from( 'my_table_name' )->join( ...