Hi I have the following insert:
$full_pjt_save = array(
'img_copertina' => $this->input->post('copertine'),
'physical_already' => $this->input->post('physical_already'),
'physical_format_product' => $this->input->post('formato_fisico'),
'physical_format' => $this->input->post('physical_format'),
'physical_format_vinile' => $this->input->post('formato_vinile'),
'physical_boxqty' => $this->input->post('physical_boxqty'),
'physical_tot_time' => $this->input->post('physical_tot_time'),
'physical_qty' => $this->input->post('physical_qty'),
'sale_price' => $this->input->post('sale_price'),
'keywords' => $this->input->post('keywords'),
'descrizione' => $this->input->post('descrizione'),
'durata' => $this->input->post('durata'),
);
$added_fields = $full_pjt_save+array('last_mod' => time());
$this->db->where('id_acquisto', $this->input->post('id_acquisto'));
$save_full_pjt_to_db = $this->db->update('progetti_'.$pjt_table, $added_fields);
$pjt_table_id = $this->db->insert_id();
This works fine, but I have a dropdown item where 'formato_vinile' is this:
45 Giri (7" Singolo, 45 Giri)
but gets inserted in the db cut after the double-quotes:
45 Giri (7
Is there a way to write it in full?
ok since i cant write it as comment cause of the code... here my idea:
its quick&dirty so you should modificate it a bit.
$added_fields = $full_pjt_save+array('last_mod' => time());
$this->db->where('id_acquisto', $this->input->post('id_acquisto'));
$this->db->set('physical_format_vinile', $this->input->post('formato_vinile'), FALSE);
$save_full_pjt_to_db = $this->db->update('progetti_'.$pjt_table, $added_fields);
$pjt_table_id = $this->db->insert_id();
note to unset/delete the key physical_format_vinile from your $added_fields array
its not tested but i hope it helps or give you an idea how to handle your problem.
*edit lets maybe check where is the exactly problem. can you do a var_dump($this->input->post('formato_vinile')) and check if the double quotes are still correct?
just escape your input $this->input->post(mysqli::escape_string ('formato_vinile'))
Apologies not a static call that was a bit of psuedo code though in CI (I am not a user)
$this->db->escape_str() ;
Related
I am attempting to query a MYSQL table with three 'scenarios' for finding objects. While I have successfully broken these into three separate queries I feel there has to be a 'better and faster' way to sift through the data. However, when I combine like below, I do not find any objects matching the query. This is using xPDO within MODx. The failed attempt is immediately below:
$orders=$modx->newQuery('Orders');
$orders->where(array(
array( //scenario #1
'Orders.start_date:<=' => $rentalDate->end_date,
'AND:Orders.start_date:>=' => $rentalDate->start_date
),
array( //scenario #2
'OR:Orders.end_date:<=' => $rentalDate->end_date,
'AND:Order.start_date:>=' => $rentalDate->start_date
),
array( //scenario #3
'OR:Orders.end_date:>=' => $rentalDate->start_date,
'AND:Orders.end_date:<=' => $rentalDate->end_date
)
));
$conflictingOrders = $modx->getCollection('Orders',$orders);
However, if I run each scenario separately, it does pick up the objects correctly. Example:
$s1Query=$modx->newQuery('Orders');
$s1Query->where(array(array('Orders.start_date:<=' => $rentalDate->end_date,'AND:Orders.start_date:>=' => $rentalDate->start_date)));
$s1Results=$modx->getCollection('Orders',$s1Query);
Any ideas where I am going wrong in the first code? Please let me know if any further information is needed. Cheers!
Helpful doc:http://rtfm.modx.com/xpdo/2.x/class-reference/xpdoquery/xpdoquery.where
The array scenarios in your code are being treated as AND conditions when listed in the $orders->where() method.
Try this:
$orders = $modx->newQuery('Orders');
$orders->where(array(
'Orders.start_date:<=' => $rentalDate->end_date,
'AND:Orders.start_date:>=' => $rentalDate->start_date
));
$orders->orCondition(array( //scenario #2
'Orders.end_date:<=' => $rentalDate->end_date,
'AND:Order.start_date:>=' => $rentalDate->start_date
));
$orders->orCondition(array( //scenario #3
'Orders.end_date:>=' => $rentalDate->start_date,
'AND:Orders.end_date:<=' => $rentalDate->end_date
));
// uncomment the following lines to see the raw query generated
// $orders->prepare();
// print_r($orders->toSql());
$conflictingOrders = $modx->getCollection('Orders',$orders);
I'm trying to create an AJAX form whereby the content of a select field populates based on the choice of a preceding select field (you see this a lot with 'country' populating 'state/province'). In my case, I want users to be able to choose their province only if active accounts exist in it.
The Javascript I can write no problem. Fetching the data is where I'm... not so much stuck as doing too much work. CakePHP likes to build select fields with options in an array of the form
$options = array(select_option_value => display_text)
My strategy, though functional, must be more convoluted than cake intended (this a is segment of a controller method).
$provinceData = $this->Account->find('all',array('recursive' => 0,
'joins' => array(
array(
'table' => 'provinces',
'type' => 'LEFT',
'conditions' => array('Account.province_id = provinces.id')
)),
'fields'=>array('provinces.id', 'provinces.name', 'provinces.abbrev'),
'conditions' => array('registration > 2')));
$provinces = array();
foreach($provinceData as $pd) {
/*note: lowercase, plural below b/c can't get 'alias' => 'Province'
to work in joins array above : ( */
$id = $pd['provinces']['id'];
$name = $pd['provinces']['name'];
$provinces[$id] = $name;
}
$this->set(compact('provinces'));
Can anyone point out a more appropriate way to do this? I assume there must be a MySQL query that can do this, but I'm pretty bad at writing elaborate MySQL queries in the first place, let alone via Cake's convention (and, for you MySQL gurus out there, I'm happy to do this from a Model->query(//MySQL code) call instead!
Any and all help truly appreciated.
Assuming the relationship Account belongsTo Province you can try this code:
$accounts = $this->Account->find(
'all',
array(
'fields' => array('Account.province_id', 'Province.name'),
'conditions' => array('Account.registration > 2'),
'group' => 'Account.province_id'
)
);
$provinces = Hash::combine($accounts, '{n}.Account.province_id', '{n}.Province.name');
$this->set(compact('provinces'));
edit: missed bracket and a period instead of an underscore . Now should work
hi im having little trouble at inserting date from drupal to mysql
here the code that i'm trying
.....
$form['kotak']['tgl'] = array(
'#type' => 'date',
'#title' => t('Tanggal'),
);
.....
function awal_form_submit($form,&$form_state){
global $user;
$entry = array(
'tanggal' => $form_state['values']['tgl'],
);
$tabel = 'jp_1';
$return = insert_form($entry,$tabel);
}
.....
function insert_form($entry,$tabel){
$return_value = NULL;
try {
$return_value = db_insert($tabel)
->fields($entry)
->execute();
}
.....
everytime i'm submit, error code like this
db_insert failed. Message = SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1, query= INSERT INTO {jp_1} (tanggal) VALUES (:db_insert_placeholder_0_month, :db_insert_placeholder_0_day, :db_insert_placeholder_0_year)
any suggestion or correction?
From the mysql error it looks like the table you created has required fields (a columns Null property is set to 0, which means that there must be a value for tha column for every row you want to insert)
Check whether there are any columns which have null set to 0.
From your example I can't see what you're trying to achieve, but in many cases it's not necessary to write into db tables manually (using db_insert()) as you can get the same result easier by creating a content type (node type) which handles a lot of functionality for you.
I hope that helps, Martin
i'm finally managed to find the answer, all i need is download "Date" module and activate its "Date API". Here the code
.....
$datex = '2005-1-1';
$format = 'Y-m-d';
$form['kotak']['tgl'] = array(
'#type' => 'date_select',
'#default_value' => $datex,
'#date_format' => $format,
'#date_year_range' => '-10:+30',
'#title' => t('Tanggal'),
);
.....
function awal_form_submit($form,&$form_state){
global $user;
$entry = array(
'tanggal' => $form_state['values']['tgl'],
);
$tabel = 'jp_1';
$return = insert_form($entry,$tabel);
}
.....
function insert_form($entry,$tabel){
$return_value = NULL;
try {
$return_value = db_insert($tabel)
->fields($entry)
->execute();
}
.....
and now i have no problem delivering to mysql.
Hope that will help other drupal newbie developer like me. Thanks :D
In my CakePHP I have ModelA which hasMany ModelB. ModelB has an int value Q.
Can I query ModelA and use containable to ensure that only those ModelB records with the maximum value for Q?
I've tried this:
$this->ModelA->contain(array(
'ModelB.Q =(SELECT MAX(ModelB.Q) FROM modelb ModelB WHERE ModelA_id = ' . $id . ')'
));
But it throws a MySQL error because CakePHP interprets the right hand side of that equality operator as a field (at least I think that's why) and so dots it.
... WHERE `Draw`.`round` =.(SELECT MAX.(`Draw`.`round`) ...
Is there a way to do this? I'd prefer not to have to drop down into $query() mode, if at all possible.
EDIT OK, after trying to follow the advice on the page that api55 suggested, I have this code:
$dbo = $this->Tournament->getDataSource();
$conditionsSubQuery['"Draw"."tournament_id"'] = $id;
$maxRounds = $dbo->buildStatement(array(
'fields' => array('MAX(Draw.round) AS prevRound'),
'table' => $dbo->fullTableName($this->Tournament->Draw),
'alias' => 'Draw',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->Tournament
);
$maxSubQuery = ' "Draw"."round" = (' . $maxRounds . ') ';
$maxSubQueryExpression = $dbo->expression($maxSubQuery);
$this->Tournament->contain(array(
'Entrant.selected = 1',
$maxSubQueryExpression
));
$tournament = $this->Tournament->read(null, $id);
But when it runs, it gives me 7 notice/warnings. The first 6 are to do with an object being passed instead of a string:
preg_match() expects parameter 2 to be string, object given
And 6 variations on this:
Object of class stdClass to string conversion
The last is less clear:
Model "Tournament" is not associated with model ""
I suspect I'm being colossally stupid, but there we go.
The contain uses conditions as a normal find, a subquery can be generated and put in conditions. So you should be able to do this as well. Try the subquery part in here and tell me how did it go ;)
This way of generating subqueries for conditions shouldn't fail :D since is the cakephp way.
If you got an error or something comment the answer to see if i can help.
In my CakePHP site, I want to make a drop-down list of all Venues, and any Restaurants that have is_venue=1.
I've tried this in my events_controller:
$venueOptions = array(
'fields' => array('id', 'name_address'),
'order' => array('name'),
'join' => array(
array(
'table' => 'restaurants',
'alias' => 'Restaurants',
'type' => 'inner',
'fields' => array('id', 'name'),
'foreignKey' => false,
'conditions' => array('restaurants.is_venue = 1')
)
),
);
$venues = $this->Event->Venue->find('list', $venueOptions);
But it appears to still just be getting the venues. I don't really need an association between the two, since their associations will both be with an event, not each other.
Where have I gone wrong? Am I close, but just need to tweak this code, or am I just all-together doing it wrong?
I think you could do something along the lines of:
<?php
....
$v = $this->Venue->find( 'list' );
$r = $this->Restaurant->find( 'list' );
$venues = Set::merge( $v, $r );
natcasesort( $venues );
// print_r( $venues );
$this->set( 'venues', $venues );
...
?>
Which is quite like the code above - I just use the Set class and make sure to Controller::set the variable to the view.
Also added some basic sorting to show you one option even though array sorting has nothing really specific to do with CakePHP.
Also fixed some bad variable names where I had originally used $venues, and $restaurants - changed to be consistently $v and $r.
Join will not work if there's no relation between. Venue and Restaurant. You should call them separately and merge the results
$venues = $this->Event->Venue->find('list', $venueOptions);
$restaurants = $this->Event->Restaurant->find('list', array('conditions' => array('is_venue' => '1')));
$results = array_merge($venues, $restaurants);
// sort results
asort($results);