Updating the data in the php code automatically from the mysql database - php

This is a part of the php code.
$params = array(
'screen_name' => 'phantom',
'count' => 5,
'exclude_replies' => true
);
I have a database(mysql) that has a table storing various screen names which keeps on updating with time automatically. Since I'm running a cron job on this php file containing this code mentioned above I wanted even this code automatically updating and containing all the names from the table automatically. Something like
$params = array(
'screen_name' => 'phantom','tuxseo','deredo',
'count' => 5,
'exclude_replies' => true
);
Is it possible?

Select the records from the database and store them in $params["screen_name"].

Related

Use HABTM using transactions in empty Database CakePHP

I'm trying to store my data in an empty database using Transactions. Now this doesn't work for me. This is my ERD: https://www.dropbox.com/s/pfakd7xplsvr7cc/db_erd.PNG
Notice that at first all three tables are empty. And I send the following array to the database:
array(
'Product' => array(
'slug' => 'c24b626d6701d3b07e30b233b989ff8811',
'product_name' => 'DAHLIA 5A',
'price_new' => '159.00',
'affiliate_url' => 'http://example.com/feed.xml',
'product_id_supplier' => 'c24b626d670133d3b07e30b2b989ff8811',
'supplier_id' => 'some_supplier',
'feedimport_id' => (int) 1
),
'Image' => array(
(int) 0 => array(
'image' => 'product_image.JPG'
)
),
'Term' => array(
'Term' => array(
(int) 0 => array(
'name' => 'Tommy Hilfiger'
)
)
)
)
Using $this->getDataSource(); . But it only fills the images and products table. If I look at the log file, it appears it tries to select from the products_terms table, after inserting the products and images table, and ofcourse it receives nothing back. And there it stops.... It doesn't try to insert in the terms table either.
Now if I look at tutorials like these: http://www.pabloleanomartinet.com/cakephp-2-x-saving-and-validating-a-habtm-relation-example/
It looks like I don't do anything wrong... But if I look closely it looks like the TAGS table in the tutorial is filled already.... So probably that's the problem? But I don't know how to get a Term ID first, before inserting the product and linking them together, using transactions....
Anyone who does?
Problem solved. It seems like the Terms table needs to be filled, else cakePHP doesn't know which term to connect to.
So the proper solution is to first check or the Term exists, else insert the Term in the Terms table, and then insert the products using HABTM (using the ID of the Term in the insert array), so the products_terms table will be filled also.

MongoDB - PHP manual references approach suitability

MongoDB newbie here. I'm having the first approach on references, and my main doubt is if i'am using the appropriate strategy(manual references) for my situation.
Working on 2 collections(user, message) in the same db, lets make an example of a document stored in user collection:
array (
'_id' => new MongoId("5231817277758e660c7202c4"),
'uname' => 'pluto',
'pwd' => new MongoInt32(1234567),
'email' => 'pluto1#gmail.com',
'phone_home' => new MongoInt64(23409238),
'phone_work' => new MongoInt64(54389724),
'phone_mobile' => new MongoInt64(9823422),
'visible' => new MongoInt32(1),
)
and an example of a document stored in message collection (sent FROM an other user TO the user above 'pluto'):
array (
'_id' => new MongoId("524358102660b2c70b8b4567"),
'from_uid' => '5231817277758e660c7202d7',
'to_uid' => '5231817277758e660c7202c4',
'object' => 'mongo manual Ref',
'content' => 'is that the correct approach?',
'datetime' => '2013-09-25 23:39:28',
)
The user logged in ('pluto') can see all the messages he received from other users but, i don't wat to print the 'from_uid' value, i want to replace it with the username of the sender.
My main doubt is if manual references is the right approach for this scenario, because with this technique(if i havn't miss understood everything), print a list of message would involve:
the 'query' for print the list of messages
an other 'query' for retrieve the username from the other collection, for each messages. Also if a user have received 1000 messages, this approach will have to run 1001 query??
My secondary doubt is if there is a way for directly merge or replace the result of two cursors
Given your use case it would probably be ok to duplicate a little bit of data and store some of the necessary fields about the sending user as an embedded document - in this case, the username.
array (
'_id' => new MongoId("524358102660b2c70b8b4567"),
'from_user' => array(
'uid' => '5231817277758e660c7202d7',
'uname' => 'mars'
),
'to_uid' => '5231817277758e660c7202c4',
'object' => 'mongo manual Ref',
'content' => 'is that the correct approach?',
'datetime' => '2013-09-25 23:39:28',
)
This approach means that when you query for messages to a user ( in which you already know the to_uid), you get all of the messages with the correct id to the from_user, but also already have their username to display.
Now, instead of querying 1000+ times, you only need to query when there is more information needed about a user - such as fetching their profile, etc...

PHRets Searches returning nothing

I have been trying to set up an automated script to pull from a RETS server using PHP and I can not get the PHRets search to return anything. My query is below:
/* Search RETS server */
$search = $rets->SearchQuery(
'Property', // Resource
3, // Class
'(LastTransDate_f77=2013-01-01+)', // DMQL
array(
'Format' => 'COMPACT-DECODED',
'Select' => 'AREA_f16',
'Count' => 1,
'Limit' => 20,
)
);
EDIT: I got this working finally. The problem had nothing to do with my code, however, it was in the MLS board's faulty documentation. The documentation said that I could select certain fields for certain property types, but that wasn't the case. I had to consult a third party developer and get the list of fields/column names that he had used.
Consult the PHRETS documentation. You have the second parameter as an integer. It should be a string. Also the comma needs to be removed after the last element in the array.
/* Search RETS server */
$search = $rets->SearchQuery(
'Property',
'3',
'(LastTransDate_f77=2013-01-01+)',
array(
'Format' => 'COMPACT-DECODED',
'Select' => 'AREA_f16',
'Count' => 1,
'Limit' => 20
));

Update multiple rows using PHP SQLBuilder

I need to edit some PHP that wasn't written by me, I've gotten most of it done but there's a bit I can't figure out the correct syntax for.
Basically we grab data from a Google Fusion Table and populate a table with it, once we've edited the data in the table we can update that information to a production version of the same table. One of the update queries looks like this:
if($table_row[12]=="New"){
$tableid1 = '1bsSleKDBdkhQZfn-oADx0tUtoOc32RqIyiX05Bo';
$insertresults = $ftclient->query(SQLBuilder::insert($tableid1,
array('SUBURB'=> $table_row[1],
'ROAD_NAME' => $table_row[2],
'DESCRIPTION' => $table_row[3],
'DIRECTION' => $table_row[4],
'STATUS' => $table_row[5],
'SITE_ID' => $table_row[6],
'COMMON_NAME' => $table_row[7],
'Lat' => $table_row[8],
'Long' => $table_row[9],
'OPERATING_DAY' => $table_row[10],
'OPERATING_HOURS' => $table_row[11],
'Version' => "current")));
$insertresults = explode("\n", $insertresults);
$rowid1 = $insertresults[1];
$updateresults = $ftclient->query(SQLBuilder::update($tableid,
array('SUBURB'=> $table_row[1],
'ROAD_NAME' => $table_row[2],
'DESCRIPTION' => $table_row[3],
'DIRECTION' => $table_row[4],
'STATUS' => $table_row[5],
'SITE_ID' => $table_row[6],
'COMMON_NAME' => $table_row[7],
'Lat' => $table_row[8],
'Long' => $table_row[9],
'OPERATING_DAY' => $table_row[10],
'OPERATING_HOURS' => $table_row[11],
'Version' => "current",
'Edited_By' => $table_row[13],
'Date_Edited' => $table_row[14]),$table_row[0]));
$updateresults = explode("\n", $updateresults);
$rowid2 = $updateresults[1];
}
What I need to do is write a similar type of SQLBuilder query that will update every record with the same SITE_ID when SUBURB is changed in one of them (i.e., if a record has SUBURB Sydney, SITE_ID 1, each record with SITE_ID 1 must be changed if one record's SUBURB value is changed from Sydney to Melbourne). Not sure exactly how to phrase this syntactically or how I would go about writing the query using SQLBuilder. Any helpw would be appreciated!
SQLBuilder is obviously a locally included class. You will need to look at its documentation or the update method itself in order to see how it expects a where clause to be coded.
The SQL for this would be:
"UPDATE tableid SET SUBURB = '{$table_row[1]}' WHERE SITE_ID = '{$table_row[6]}'"
If you are able to run pure SQL then that will do it. Otherwise I'd need to see the Class libraries.

CakePHP findList doesn't return aggregated values

The following query returns an array containing the proper ids, but null for all values.
If I remove the aggregation function (AVG()), it returns values (not the averaged ones of course), if I choose e.g. find('all') it returns the average, but not in the list format I want (I could work with that, but I want to try to do it with 'list' first).
$progress = $this->Trial->find('list', array(
'fields' => array(
'Trial.session_id',
'AVG(Trial.first_reaction_time_since_probe_shown) AS average_reaction_time'
),
'group' => 'Trial.session_id',
'conditions' => array(
'Trial.first_valid_response = Trial.probe_on_top',
'TrainingSession.user_id IS NOT NULL'
),
'contain' => array(
'TrainingSession' => array(
'conditions' => array(
'TrainingSession.user_id' => $this->Auth->user('id')
)
)
),
'recursive' => 1,
));
The generated SQL query returns exactly the result I want, when I send it to the DB via PhpMyAdmin.
SELECT
`Trial`.`session_id`,
AVG(`Trial`.`first_reaction_time_since_probe_shown`) AS average_reaction_time
FROM
`zwang`.`trials` AS `Trial`
LEFT JOIN
`zwang`.`training_sessions` AS `TrainingSession` ON (
`Trial`.`session_id` = `TrainingSession`.`id` AND
`TrainingSession`.`user_id` = 1
)
WHERE
`Trial`.`first_valid_response` = `Trial`.`probe_on_top`
GROUP BY
`Trial`.`session_id`
I've examined the source for find('list'). I think it's due to the "array path" for accessing the list getting screwed up when using functions in the query, but I couldn't fix it yet (or recognise my abuse of CakePHP logic).
Once I posted the question, Stackoverflow started relating the correct answers to me.
Apparently, it can't be done with 'list' without virtualFields.
I didn't expect that because it worked using the other find-types.
$this->Trial->virtualFields = array(
'average_reaction_time' => 'AVG(Trial.first_reaction_time_since_probe_shown)'
);
$progress = $this->Trial->find('list', array(
'fields' => array('Trial.session_id','average_reaction_time')
/* etc... */
));

Categories