Update the value of a field in database by 1 using codeigniter - database

I want to implement a SQL statement using codeigniter active record.
UPDATE tags SET usage = usage+1 WHERE tag="java";
How can I implement this using Codeigniter active records?

From the documentation:
set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE.
So this should work to pass the increment statement directly to the database:
$this->db->set('usage', 'usage+1', FALSE);
$this->db->where('tag', 'java');
$this->db->update('tags');
Because it's not escaped, if you're using a variable instead of a fixed number it should be verified as numeric beforehand.

You can also use something like this
$data = array('usage' => 'usage+1', *other columns*);
$this->db->where('tag', 'java');
$this->db->update('tags', $data);
UPDATE: $data was not being passed on to update

I find its sometimes simpler to just write the SQL rather than having Active Record build it for me.
$sql = 'update tags set usage=usage+1 where tag=?';
$this->db->query($sql, array($tag));

Related

TYPO3: How do i change a part of a string in the database with QueryBuilder?

I am using TYPO3 10 and i would like to change a specific text/word inside the pi_flexform column in the tt_content table.
The concept: I want to migrate the forms folder from user_upload to form_definitions (Updating a Project from TYPO3 8 to 10) using the QueryBuilder.
I could just override the form_definitions folder back to the user_upload, but i want a clean structure. Now if i change the folder from where the forms are coming, the forms do not work anymore because in the database and in the pi_flexform column exists the following:
...
<field index="settings.persistenceIdentifier">
<value index="vDEF">1:/user_upload/NameOfTheForm.form.yaml</value>
</field>
...
What i want, is to change the 1:/user_upload/ to 1:/form_definitions/.
I know i will have to use the UPDATE & SET with QueryBuilder but i do not know how to use it for a specific string inside a string.
How do i achieve this?
In order to replace a specific text/word in a column, one can use QueryBuilder's UPDATE & SET functions. It looks like this:
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder
->update('tt_content')
->where(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->like(
'tt_content.pi_flexform',
$queryBuilder->createNamedParameter(
'%user_upload%',
Connection::PARAM_STR
)
)
)
)
->set(
'tt_content.pi_flexform',
sprintf(
'REPLACE(tt_content.pi_flexform, %s, %s)',
$queryBuilder->createNamedParameter('user_upload', Connection::PARAM_STR),
$queryBuilder->createNamedParameter('form_definitions', Connection::PARAM_STR)
),
false
)
->execute();
Breaking the code down:
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
We initialise the query and we are removing all the restrictions. QueryBuilder only performs to the entries that are not set to deleted=1 or hidden =1. We want to perform the updated path to all entries, so we remove the restrictions.
$queryBuilder->expr()->like(
'tt_content.pi_flexform',
$queryBuilder->createNamedParameter(
'%user_upload%',
Connection::PARAM_STR
)
)
We only want to update the entries that in the pi_flexform column the word user_upload exists.
->set(
'tt_content.pi_flexform',
sprintf(
'REPLACE(tt_content.pi_flexform, %s, %s)',
$queryBuilder->createNamedParameter('user_upload', Connection::PARAM_STR),
$queryBuilder->createNamedParameter('form_definitions', Connection::PARAM_STR)
),
false
)
->execute();
We use the SQL REPLACE command to find the word and replace it. With the $queryBuilder->createNamedParameter() we avoid SQL injections.
Hope it helped
Best regards
You can also use a raw DB query with
$connection->query('Your raw query');
Be aware that this will only work for the specific database type and you need to take care about security yourself.

bulk remove the required paramater fron custom option magento

i have around 10000 products in my database which all have custom options and they all are set to required.
I need to unset required.
Please suggest me how can i do this dynamically.
I don't want to do it one by one from magento admin because that will take forever
thanks
If you want to reset the required field for all custom options of all products here is a quick & dirty way of doing it.
All the custom options are stored in the table catalog_product_option.
The column name that decides if the option is required or not is is_require.
So running this query should do the trick.
UPDATE `catalog_product_option` SET `is_require` = 0 WHERE 1
Add the table prefix if you have one.
you can update those product's required parameter by using import product by csv. I think, you have imported products by csv. simply copy that SKU and custom field option into new csv file.
And import Again those products. It will update custom option required field as you want
I'm just expanding on Marius' answer. I needed to do this but only for a bunch of custom options I'd made required by accident. I figure it might help someone else who made a similar miscalculation! :-D
UPDATE catalog_product_option o
JOIN catalog_product_option_title t ON t.option_id=o.option_id
SET o.is_require = 0
WHERE t.title = 'Additional Comments'
AND o.is_require = 1
Luckily the options I imported all had the same title so I was able to filter it that way.
If you're using SKU's for your custom options, you could probably further simplify the query as it requires no joins.
UPDATE catalog_product_option o
SET o.is_require = 0
WHERE o.sku = 'SKU1234'
AND o.is_require = 1
^ not tested that but looking at the structure of the table that should work.

Codeigniter run query before a update

I have to run a query after every update and I want to know if there is a way to automate a $this->db->query() before every $this->db->update()
I'm using it for log.
you can write your own function in the file core/MY_Model.php to do that:
function queryThenUpdate($query,$update)
{
$query = $this->db->query($query);
//use as you need $query
$this->db->update($update['table'],$update['data']);
}
where:
$query is your actual query: SELECT * FROM ...
$update is an array of two elements $update['table'] is the table to update and $update['data'] is the updating data as specified on codeigniter active record's documentation
then make every model extend MY_Model
class Your_Model extend MY_Model
and every time you need to update something:
$this->Your_Model->queryThenUpdate($query,$update)
I believe you want to use codeigniters 'hook'

In Symfony 1.2 how can I get the current database name from the database.yml file in Propel?

I have a raw sql query I need to run, but the database name changes in each environment (live: db, dev db_test)
I need to get the current database name from the databases.yml file.
How can I get just the current database name?
I am using the Propel ORM
Initially I thought this would be pretty easy via sfPropelDatabase::getConfiguration() but that returns an array. As such, I had to parse the result to get the data, and I think there's probably a better way than this:
$propel_config = sfPropelDatabase::getConfiguration();
preg_match('/dbname=([^;]+);/', $propel_config['propel']['datasources']['propel']['connection']['dsn'], $matches);
echo $matches[1];
Anyone got anything better?
The following code works in Propel2 -- essentially the same as the accepted answer.
$mgr = \Propel\Runtime\Propel::getConnectionManager($connectionId);
$dsn = $mgr->getConfiguration()['dsn'];
preg_match('/dbname=([^;]+)/', $dsn, $matches);
echo $matches[1];

How to Get Field Name With Query in Zend Framework

How to Get Field Name With Query in Zend Framework
Test "Select * From Test1,Test2"
how to get all field name in this query
Zend Frame work cam do it?
Untested, but I believe the query is returned as an associative array (where the column name is the key), and so you can loop through the first record and pick up the column names, e.g.
$sql = 'Select * From Test1,Test2';
$result = $db->fetchAll($sql, 2);
foreach ($result[0] as $key => $value) {
echo $key;
...
}
You could also issue $db->describeTable('Test1'), etc. before or after the query which would provide you with all the meta information you need. This query is pretty expensive though, so make sure to cache the response.
Also, if you're using a model which extends Zend_Db_Table_Abstract, then you should have all this information already. In this case, all you need to do is access the protected property $_metadata.
HTH

Resources