$queryBuilder->createNamedParameter in update query not working (TYPO3 10.4) - typo3-10.x

In a custom utility class (no controller or repository) I have the following UPDATE-Query:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('my_table');
$uid = 1;
$val = 'Test';
$queryBuilder
->update('my_table')
->set('title', $queryBuilder->createNamedParameter($val))
->where(
$queryBuilder->expr()->eq('uid', $uid)
)
->execute();
When the update is done, the title in the database record has the value ":dcValue1" and not "Test".
Why does the prepared statement not resolve the parameter $val?

Related

TYPO3 Extbase Join

I'm try to make this query with extbase work:
SELECT a.*
FROM tx_apartments_domain_model_apartment a
LEFT
JOIN tx_apartments_domain_model_booking b
ON b.apartment = a.uid
AND b.start <= '2018-07-23'
AND b.end >= '2018-07-21'
WHERE b.uid IS NULL AND a.hidden=0 AND a.deleted=0;
the following code does not work, i get an empty result:
/** #var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_apartments_domain_model_apartment');
$statement = $queryBuilder
->select('a.*')
->from('tx_apartments_domain_model_apartment','a')
->leftJoin(
'a',
'tx_apartments_domain_model_booking',
'b',
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('b.apartment','a.uid'),
$queryBuilder->expr()->lte('b.start', '2018-07-23'),
$queryBuilder->expr()->gte('b.end', '2018-07-21')
)
)
->where(
$queryBuilder->expr()->isNull('b.uid')
)
->execute();
//DebuggerUtility::var_dump($queryBuilder->getSQL());
return $statement->fetchAll();
can anybody help me?
What kind of records do you expect to get?
as uid is a mandantory field you never have records where the uid is NULL.
If you want to access new records which are not stored to the database yet: they get a faked uid like NEW123
This works for me:
/** #var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_apartments_domain_model_apartment');
$queryBuilder
->getRestrictions()
->removeAll();
$statement = $queryBuilder
->select('a.*')
->from('tx_apartments_domain_model_apartment','a')
->leftJoin(
'a',
'tx_apartments_domain_model_booking',
'b',
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('b.apartment','a.uid'),
$queryBuilder->expr()->lte('b.start', $queryBuilder->createNamedParameter('2018-07-23')),
$queryBuilder->expr()->gte('b.end', $queryBuilder->createNamedParameter('2018-07-21'))
)
)
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->isNull('b.uid'),
$queryBuilder->expr()->eq('a.hidden',0),
$queryBuilder->expr()->eq('a.deleted',0)
)
)
->execute();
$results = $statement->fetchAll();
// DebuggerUtility::var_dump($queryBuilder->getSQL());
// DebuggerUtility::var_dump($results);
return $results;
I do not know where exactly you are using this code. Typically I would think that retrieving records from the database should happen in a repository.
If you are using this in the ApartmentRepository and if there is a relation properly configured between apartments and bookings (1:n, I would assume) in the TCA, then you could use something like this:
public function findInDateRange ($dateConstraint)
{
$query = $this->createQuery();
$constraints = [];
$constraints[] = $query->lessThanOrEqual(
'bookings.start',
$dateConstraint['start']
);
$constraints[] = $query->greaterThanOrEqual(
'bookings.end',
$dateConstraint['end']
);
$query->matching($query->logicalAnd($constraints));
return $query->execute();
}
I deliberately left out the b.uid IS NULL part here, as that should always return nothing since uid should be AUTO_INCREMENT. Unless the default query settings are changed, there is also no need to check the disable fields (hidden, deleted) manually.
Note that in order for this to work, the Apartment Domain Model needs a field bookings with a corresponding column in the database (that is something I stumbled over last year).
As a side-note: Is it intentional that you allow for the end to be before the start in your query (start: 2018-07-23, end: 2018-07-21), or is that just exemplary?

Doctrine query with ManyToMany relation

I have a simple relation ManyToMany between two entities "article" and "category" (for a blog). I would like to fetch every article with a particular category, but I don't know how to create that query. What I did is :
$query = $em->createQuery(
'SELECT u
FROM SiteBlogBundle:article u
WHERE u.categorie.name = :cat
ORDER BY u.date DESC'
)
->setParameter('cat', '$cateogory')
The line WHERE u.categorie.name = :cat doesn't work. How can I achieve that?
If you are okay with using a query-builder for better readability instead of DQL:
$articles = $em
->getRepository('SiteBlogBundle:Article')
->createQueryBuilder('article')
->leftJoin('article.category', 'category')
->where('category.name = :category_name')
->orderBy('article.date', 'DESC')
->setParameter('category_name', $category->getName())
->getQuery()
->getResult()
;
But i would strongly advise you to put this query into the repository like this:
// ArticleRepository.php
public function findArticlesByCategoryName($name)
{
return $this->createQueryBuilder('article')
->leftJoin('article.category', 'category')
->where('category.name = :category_name')
->orderBy('article.date', 'DESC')
->setParameter('category_name', $name)
->getQuery()
->getResult()
;
}
Then inside your controller just do:
public function getArticles(Category $category)
{
$em = $this->getDoctrine()->getEntityManager();
return $em->
getRepository('SiteBlogBundle:Article')
->findArticlesByCategoryName($category->getName())
;
}

sql update codeigniter

I am using codeIgniter..
I want to update a table column is_close when id=$ticket_id of my table= tbl_tickets.
I am doing this :-
$data=array(
'is_close'=>1
);
$this->db->where('id',$title_id);
$this->db->update('tbl_tickets',$data);
and I have also done this :-
$sql = "UPDATE tbl_tickets SET is_close={1} WHERE id='$title_id'";
$this->db->query($sql);
both are not working,i.e., my table is not updating the value to 1 and also no error is being shown in the broswer. :(
Edited: Included my model part :
function setClosePost($title_id){
$sql = "UPDATE tbl_tickets SET is_close=0 WHERE id='$title_id'";
$this->db->query($sql);
// $data=array(
// 'is_close'=>1
// );
// $this->db->where('id',$title_id);
// $this->db->update('tbl_tickets',$data);
}
My controller :-
function closePost(){
$this->load->model('helpdesk_model');
$this->helpdesk_model->setClosePost($this->input->post('title_id'));
}
first of all use a get method to check if ticket_id is exist or not.
another thing is always use return in your functions in models so you can check them by if(function_name){...}else{...}
then if your get method returned data correctly try
Model Method
public function set_closed($ticket_id){
$this->db->set(array(
'is_close'=>1
)); // pass fields in array
$this->db->where('id',$ticket_id);
$this->db->update('tbl_tickets'); // table name
return true;
}
then check that in your controller
if($this->Ticket_model->set_closed($ticket_id) == true){
echo 'ticket set to closed correctly';
}else{
echo 'there is some error on updating database'.$this->db->error(); // to checkout db error .
}
First, check $title_id before passing:
var_dump($title_id);
Then, try do "select a row with this id" before updating and after.
$query = $this->db->get_where('tbl_tickets', array('id' => $id));
foreach ($query->result() as $row)
{
var_dump($row->is_close);
}
$data=array(
'is_close'=>1
);
$this->db->where('id',$title_id);
$this->db->update('tbl_tickets',$data);
$query = $this->db->get_where('tbl_tickets', array('id' => $id));
foreach ($query->result() as $row)
{
var_dump($row->is_close);
}
Then, give your table structure.
Just try like this
$sql = "UPDATE tbl_tickets SET is_close='1' WHERE id=".$title_id;
$this->db->query($sql);
just try like this
**function edit($close,$id) {
$sql = "UPDATE tbl_tickets SET is_close= ? WHERE id = ? ";
$this->db->query($sql, array($close,$id));
}**
To handle this type of errors, i mean if reflection is not happen in database, then use below steps to resolve this type of error.
1) use $this->db->last_query() function to print query, using this we can make sure our variable have correct value (should not null or undefined), using that we can make sure also SQL query is valid or not.
2) If SQL query is valid then open phpmyadmin & fire same query into phpmyadmin, it will return error if query columns or table names are invalid.
Use this way, its best way to cross check our SQL queries issues.
I hope it will work.
Thanks
You are trying to update integer(INT) type value, just cross check with your column datatype if that is varchar then you have to put value in a single or double quote.
Like this
$data=array('is_close'=> '1');

Write an update query where ID is X, and name is either foo or bar in zf

I'd like to write the following SQL update query with Zend Framework:
UPDATE my_table
SET my_field
WHERE name = 'John'
AND (other_field = 'foo' OR other_field = 'bar')
How to do this with Zend Framework?
Assuming you have a Model file for my_table ..if not create one inside your model directory.
<?php
class Yournamespace_Model_Mytable extends Zend_Db_Table_Abstract{
protected $_name = "my_table";
public function updateFunction(){
$data = array('my_field' => 'new value to be set');
$where[] = 'name = John';
$where[] = '(other_field = "foo" OR other_field = "bar")';
$update= $this->update($data, $where);
}
}
just call in any action like this
$obj = new Yournamespace_Model_Mytable();
$obj->updateFunction();

How to get particular column in zend using Left join

I am new to zend framework,
Following is the plain mysql query which takes particular column from table,
SELECT jobs_users.id,jobs_users.first_name from jobs_users left join friends on jobs_users.id=friends.friend_id where friends.member_id=29
I tried with zend to implement the above query like below,
public function getFriendsProfileList($id){
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from('jobs_users')
->joinLeft(
'friends',
'jobs_users.id=friends.friend_id',
array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo')
)
->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;
}
Here i got result with all column name , not with exact column name which i have given in query.
Kindly help me on this.
Use this instead:
$select->from('jobs_users', array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'))
->joinLeft('friends', 'jobs_users.id=friends.friend_id')
->where("friends.member_id = ?", '20');
You may also try this:
$select = $db->select();
$select->setIntegrityCheck(false);
$select->joinLeft('jobs_users','',array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'));
$select->joinLeft('friends','jobs_users.id=friends.friend_id', array());
$select->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;