草庐IT

find_or_initialize_by

全部标签

php - CakePHP 返回 find ('all' ) 使用 'id' 作为数组索引

因此,我尝试使用“产品”的ID作为每个产品的索引键来返回一个查找“全部”数组。通常它返回:array((int)0=>array('Product'=>array('id'=>'1232','category_id'=>'330','name'=>'Product#1',)),(int)1=>array('Product'=>array('id'=>'1245','category_id'=>'310','name'=>'Product#2',)),(int)2=>array('Product'=>array('id'=>'1248','category_id'=>'312','nam

PHP 使用 OR 运算符检查多个值的值

我有一个文件名($fname),我需要将$pClass分配给文件类型,之后加上“-”。目前我总是得到text-,不管它是什么文件类型。//Thisgetstheextentionforthefileandassignstheclasstotheicon$pieces=explode('.',$fname);$ext=array_pop($pieces);if($ext==(('txt')||('rtf')||('log')||('docx'))){$pClass='text-';}elseif($ext==(('zip')||('sitx')||('7z')||('rar')||('g

PHP preg_split : Split string by other strings

我想用一系列单词拆分一个大字符串。例如$splitby=array('these','are','the','words','to','split','by');$text='Thisisthestringwhichneedstobesplitbytheabovewords.';那么结果就是:$text[0]='Thisis';$text[1]='stringwhichneeds';$text[2]='be';$text[3]='above';$text[4]='.';我该怎么做?是preg_split最好的方法,还是有更有效的方法?我希望它尽可能快,因为我将拆分数百MB的文件。

php - Magento : Get country code by country name

我试图通过国家名称查找国家代码。所以,例如,我有“荷兰”,我需要得到“NL”我知道有一种方法可以找到名称形式的代码:$country_name=Mage::app()->getLocale()->getCountryTranslation($country_code)但我需要反之亦然。那么Magento中有什么方法可以解决吗? 最佳答案 Fromtheotherquestion,这只能通过循环国家集合来完成$countryId='';$countryCollection=Mage::getModel('directory/count

php - 谷歌地图 PHP CURL 问题 "Your client has issued a malformed or illegal request. That’ s 我们都知道。”

我有一个使用CURL和GoogleMapsAPI返回地址坐标的函数。代码如下:functionget_coordinates($address_string){$address=urlencode($address_string);$url="https://maps.googleapis.com/maps/api/geocode/json?address=".$address."&key=".$api_key;$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_RETURNTRANSFER

php - 是否有 PHP Array Group By 函数?

$arr1=array('a'=>'1','b'=>'blah','c'=>'whatever...','aa'=>'2','bb'=>'lbha','cc'=>'everwhat...','dd'=>'bingo','aaa'=>'3','bbb'=>'halb','ccc'=>'revetahw...');在数组中我有三个不同的索引长度a,b和c的长度都是1。aa,bb,cc和dd的长度都是2。而aaa,bbb,ccc的长度都是3。我想做的是找到元素最多、长度最长的索引(按长度分组)。所以我会使用aa、bb、cc、dd,因为它们有4个元素,这将返回索引长度2。我想知道如何获得2?这

php - 在 php 中使用 or 运算符?

以下代码:$result=(falseortrue);echo("Withextraparentheses:".($result?"true":"false"));$result=falseortrue;echo("Withnoparentheses:".($result?"true":"false"));生成输出:Withextraparentheses:trueWithnoparentheses:false我不明白为什么。php不应该评估$result=falseortrue;首先测试false然后,因为它不是真的,继续评估true?如有任何建议,我们将不胜感激。

php - 错误 : Call to a member function find() on a non-object in cakephp controller

我是cakephp的新手,并尝试使用控制台工具生成一些CRUD操作。它工作正常,除了一张table(最大的)。尝试添加新元素时,返回:Error:Calltoamemberfunctionfind()onanon-objectFile:C:\wamp\www\cakephp\app\Controller\ChantiersController.phpLine:50这是第50行及以后的代码:$programs=$this->Chantier->Program->find('list');$etats=$this->Chantier->Etat->find('list');$types=$

PHP preg_split : Split string by forward slash

我想使用PHP函数preg_split通过正斜杠拆分我的字符串192.168.1.1/24.我的变量:$ip_address="192.168.1.1/24";我试过了:preg_split("/\//",$ip_address);//Andpreg_split("/[/]/",$ip_address);Errormessage:preg_split():Delimitermustnotbealphanumericorbackslash我在stackoverflow中找到了以下答案Phppreg_splitforforwardslash?,但它没有提供直接的答案。

php - 哪个更好 : filter using SQL or PHP?

情况:每个用户只能看到特定国家和特定代理商的销售报告。那么,哪个更好:$reports=$DB->select('fields'=>'*','table'=>'sales','where'=>array('sales_date'=>array('2011-06-02','2011-06-04'),'sales_country'=>array_keys($allow_country),'sales_agent'=>array_keys($allow_agent)));或者:$result=$DB->select('fields'=>'*','table'=>'sales','where'