FindAll() not working in Yii Framework - php

My model name is Sales.
$allSales = Sales::model()->findAll();
$allSales return nothing (Blank). But its working my local computer(Ubuntu) and Not working on live server (Mac).
$allSales2 = Sales::model()->findAll("id < 2000");
$allSales2 is working on both server
Please help me. Thanks in advance.

(Blank) is vague, but if the page itself is blank when it loads you are likely seeing an out of memory error. This is common with a large number of active records being queried at once. Check the php_error.log file and the respective memory limits in php.ini on each server.
You can also try to use CDataProviderIterator to fetch all the models, instead of findAll().
$dataProvider = new CActiveDataProvider('Sales');
$allSales = new CDataProviderIterator($dataProvider);
foreach ($allSales as $model) {
//do whatever
}
If your issue IS a memory problem, this should get around it.
If not, add var_dump($allSales); to your original code, and report the results from the live server.

You need pass the conditions as array. Try like this
$allSales2 = Sales::model()->findAll(
array(
"condition" => "id < 2000"
)
);
If you do not like to use array in findAll then can use CDbCriteria like below
$criteria = new CDbCriteria;
$criteria->select = '*';
$criteria->condition = 'id < 2000';
$allSales2 = Sales::model()->findAll($criteria);

Related

Detect Languages; CakePHP updateAll Bad Performance

UPDATE: I think the cakePhp updateAll is the problem. If i uncomment the updateAll and pr the results i get in 1-2 seconds so many language Detections like in 5 minutes!!!! I only must update one row and can determine that row with author and title... is there a better and faster way???
I'm using detectlanguage.com in order to detect all english texts in my sql database. My Database consists of about 500.000 rows. I tried many things to detect the lang of all my texts faster. Now it will take many days... :/
i only send 20% of the text (look at my code)
i tried to copy my function and run the function many times. the copied code shows the function for all texts with a title starting with A
I only can run 6 functions at the same time... (localhost)... i tried a 7th function in a new tab, but
Waiting for available socket....
public function detectLanguageA()
{
set_time_limit(0);
ini_set('max_execution_time', 0);
$mydatas = $this->datas;
$alldatas = $mydatas->find('all')->where(['SUBSTRING(datas.title,1,1) =' => 'A'])->where(['datas.lang =' => '']);
foreach ($alldatas as $row) {
$text = $row->text;
$textLength = round(strlen($text)*0.2);
$text = substr($text,0,$ltextLength);
$title = $row->title;
$author = $row->author;
$languageCode = DetectLanguage::simpleDetect($text);
$mydatas->updateAll(
['lang' => $languageCode], // fields
['author' => $author,'textTitle' => $title]); // conditions*/
}
}
I hope some one has a idea for my problem... Now the language detection for all my texts will take more than one week :/ :/
My computer runs over 20 hours with only little interruptions... But i only detected the language of about 13.000 texts... And in my database are 500.000 texts...
Now i tried sending texts by batch, but its also to slow... I always send 20 texts in one Array and i think thats the maximum...
Is it possible that the cakePhp 3.X updateAll-function makes it so slowly?
THE PROBLEM WAS THE CAKEPHP updateAll
Now i'm using: http://book.cakephp.org/3.0/en/orm/saving-data.html#updating-data with a for loop and all is fast and good
use Cake\ORM\TableRegistry;
$articlesTable = TableRegistry::get('Articles');
for ($i = 1; $i < 460000; $i++) {
$oneArticle = $articlesTable->get($i);
$languageCode = DetectLanguage::simpleDetect($oneArticle->lyrics);
$oneArticle->lang = $languageCode;
$articlesTable->save($oneSong);
}

Save Multiples times Cakephp 3.x

What I want to do is to save my data multiple times. In order to do that I put this code in my Controller :
foreach ($tw->statuses as $t) {
$tw->image = $t->user->profile_image_url;
$tw->name = $t->user->name;
$tw->screenname = $t->user->screen_name;
$tw->message = $t->text;
$tw->Fil_id = $FiltreId;
$this->Twes->save($tw);
}
return $this->redirect(['action' => 'index']);
However when the execution is done, I found only the last record saved in my database, and not the other ones. Can anyone help me ?
This can be happening due to various things:
The variable $tw->statuses only contains ONE status.
The table where you store the data has a unique index that collides when the data is inserted for the second time.
Can you give more information (like the table description, or a var_dump of the named variable before iterating it) to exactly pinpoint what's happening?
I found where the problem was. I updated my code, and i used TableRegistry and used newEntity inside my foreach as you can see :
$oPeople = TableRegistry::get('Tweets');
$oQuery = $oPeople->query();
foreach ($tw->statuses as $t) {
$test = array($FiltreId,$t->text,$t->user->name,$t->user->screen_name,$t->user->profile_image_url,null,null,null,null,null);
$tw = $oPeople->newEntity($test);
$tw->image = $t->user->profile_image_url;
$tw->name = $t->user->name;
$tw->screenname = $t->user->screen_name;
$tw->message = $t->text;
$tw->Fil_id = $FiltreId;
$this->Twes->save($tw);
}
return $this->redirect(['action' => 'index']);
This way all the data is saved, and not only the last one.

how to output a collection of data from mongodb sort by date

I want to retrieve 10 rows of latest news from mongodb.
First I need to sort the data by the field 'timestamp' by ascending order. Then I need to choose the top 10 rows which are the rows with the latest timestamp.
This is how I establish my connection (successfully)
$m = new MongoClient("mongodb://127.0.0.1");
$db = $m ->nutch;
//echo "Database nutch selected";
$collection = $db -> crawl_data;
$cursor = $collection->find();
This is how I tried to get the data following the PHP manual guide
$cursor->sort(array('timestamp' => 1));
foreach($cursor as $doc){
echo $doc["title"];
}
FYI: the data type of timestamp is string: "2015/01/31". I am not sure if this is the reason.
Also, When I do php with MySql, the browser always tells me at which line the problem is. With mongodb, it does not give you any error reporting except a blank page....
Sort doesn't work like this anymore,
To be able to sort with find, you simply use the second find parameter like this:
$filter = [];
$options = ['sort' => ['timestamp' => -1]];
$client = new MongoDB\Client('mongodb://localhost');
$client->mydb->mycollection->find($filter, $options);
copied from this answer
The php syntax is a bit confusing.
The Sort() and Limit() methods can be done on the find (regardless of order the sort will always happen first).
It would look something like this:
$cursor = $collection->find ()->sort(array('timestamp'=>-1))->limit(10);
And then you can reverse the order of the 10 documents in php, or you would probably need to use the aggregation framework.
You can use the _id field to sort by timestamp.
The following query can print the latest 10 records.
$cursor = $collection->find()->sort( array("_id" => -1 ))->limit(10);

Yii using a variable with an IN condition

I am trying to pull information into a page using my model. The issue is that I need to use an IN condition on my mysql using a variable.
Here is the code I use currently
$list_id = '1,3';
$clients = ListSubscriber::model()->findAll(array('condition'=>'list_id IN (:list_id)','params'=>array(':list_id'=>$list_id)));
I won't necessarily know how many numbers will be stored within $list_id, hence the need for a variable to work with the IN.
The code does execute without errors, but only seems to return the values for the first number of $list_id, so in this case it only finds users where the list_id = 1.
Any help is appreciated. I have found this question Yii addInCondition
However they are using static values, which does not resolve my issue.
When I do use static values, the code executes with results as expected.
You can use addInCondition :
$list_id = '1,3';
$criteria = new CDbCriteria();
$arr_list_id = explode(",",$list_id);
$criteria->addInCondition("list_id ", $arr_list_id );
$clients = ListSubscriber::model()->findAll($criteria);
$list_ids = array(1,3);
$clients = ListSubscriber::model()->findAllByAttributes(array('list_id'=>$list_ids));

Kohana Model Saved Twice

I just installed a fresh copy of Kohana 3.2, built my database, wrote my first model, and tried testing it. Everything works fine except the model's "save" method is being executed twice--I end up with two new entries in the database instead of one. The problem only occurs when I use the "find" code shown below.
Why would the model's save get executed twice, once as expected and once because of the find?
Here's the code:
class Controller_Welcome extends Controller {
public function action_index()
{
$rating = ORM::factory('rating');
$rating->user_id = 1;
$rating->userlevel_id = 3;
$rating->category_id = 1;
$rating->page_id = 1;
$rating->rating = 4;
$rating->comments = 'This one is a real killer';
$rating->ratingstatus_id = 1;
$rating->save();
$found = ORM::factory('rating')
->where('id', '=', 1)
->find();
$this->response->body($found->comments); // Test to check for found data
}
} // End Welcome
Thanks in advance!
There are two issues that were causing my problem:
I didn't have a favicon.ico on my server. Many browsers request one, and all URLs that aren't actual files or directories get redirected to the index page. Every time I loaded the page, the browser would request a missing favicon and get redirected to my index page--two requests. After looking at my logs, this page was what tipped me off: http://forum.kohanaframework.org/discussion/7447/error-kohana_request_exception/p1
After I added a favicon, I still saw the double request behavior occasionally. It turns out it was a behavior of Google Chrome--Chrome prefetches pages, so each time I changed the content, Chrome would prefetch and cache the page (adding a request).
After adding a favicon and when using a browser besides Chrome, everything behaves as expected.
$rating = ORM::factory('rating');
This line represents nothing.
If you want to create new record you should use create() instead save().
$rating = new Model_Rating;
$rating->user_id = 1;
$rating->userlevel_id = 3;
$rating->category_id = 1;
$rating->page_id = 1;
$rating->rating = 4;
$rating->comments = 'This one is a real killer';
$rating->ratingstatus_id = 1;
$rating->create();
If you want to load single rating object with given id:
$found = ORM::factory('rating', 1);

Categories