我在视图表中为MYSQL中的产品做了一些计算,所以我列出了这些id作为产品参考ID.
$referenceIds = viewTable::orderBy('score', 'DESC')->lists('product_id');
Run Code Online (Sandbox Code Playgroud)
我想从这些参考ID中获取产品.
$products = Product::whereIn('id', $rederenceIds);
Run Code Online (Sandbox Code Playgroud)
产品内容是正确的,但顺序错误,如何通过引用ID加载产品订单,我知道我可以在MySQL中使用ORDER BY FIELD(id,' . $fields . ')
,但我想找出Laravel方式,并希望不要使用ORDER BY FIELD
命令,因为它似乎浪费提高数据库效率.
例如,与$ variable === true有什么不同?
<?php
if (true === $variable) {
//
}
if (1 === intval($variable)) {
//
}
Run Code Online (Sandbox Code Playgroud) class ChanTest extends PHPUnit_Framework_TestCase
{
protected $db;
protected function setUp()
{
$this->db = new Core\Database('unitest');
}
/**
* @dataProvider testProvider
*/
public function testData($a, $b, $c)
{
$this->assertEquals($a + $b, $c);
}
public function testProvider()
{
$this->db->query('SELECT `a`, `b`, `c` FROM `units`');
return $this->db->rows();
}
}
Run Code Online (Sandbox Code Playgroud)
class ChanTest extends PHPUnit_Framework_TestCase
{
protected $db;
protected function setUp()
{
$this->db = new Core\Database('unitest');
}
public function testData($a, $b, $c)
{
$this->db->query('SELECT `a`, `b`, `c` FROM `units`');
foreach ($this->db->rows() as $item) …
Run Code Online (Sandbox Code Playgroud) 我想将一个字符串拆分成字符,并使每个字符均匀地容纳容器,这是我暂时的工作:http://jsfiddle.net/d5fu9/
第一个项目必须附加到左侧,最后一个项目必须附加到右侧.
$.fn.textjustify = function() {
return this.each(function() {
var text = $(this).text(),
containerWidth = $(this).width(),
character = '',
string = '',
singleWidth = 0,
firstItemWidth = 0,
lastItemWidth = 0,
alignWidth = 0;
if ('' !== text) {
$(this).css('position', 'relative');
textArray = text.split('');
singleWidth = Math.floor(containerWidth / textArray.length);
for (i in textArray) {
// Wrapp with div to get character width
character = ('' === $.trim(textArray[i])) ? ' ' : textArray[i]; …
Run Code Online (Sandbox Code Playgroud) 命令
phpmd app,config,routes text phpmd.xml
Run Code Online (Sandbox Code Playgroud)
我在 Laravel 项目上使用 phpmd,在 design.xml 下有一个名为的规则CouplingBetweenObjects
,我想忽略 app/Http/Controllers 目录中的所有文件到此规则,phpmd 只提供--exclude
使该目录通过所有规则的选项,是否可能通过 XML 文件或命令行忽略特定目录的规则
phpmd.xml
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Netask rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Netask code style rule set
</description>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity"/>
<rule ref="rulesets/codesize.xml/NPathComplexity"/>
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassLength"/>
<rule ref="rulesets/codesize.xml/ExcessiveParameterList"/>
<rule ref="rulesets/codesize.xml/ExcessivePublicCount">
<properties>
<property name="minimum" value="30"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyFields">
<properties>
<property name="maxfields" value="20"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/TooManyMethods"/>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
<properties>
<property name="maximum" value="30"/>
</properties>
</rule>
<rule ref="rulesets/controversial.xml"/>
<rule …
Run Code Online (Sandbox Code Playgroud) 我有三个表user
,role
,permission
,user
的hasMany role
,role
的hasMany permission
,我想找出用户谁拥有我给了允许,所以我用whereHas
$user = User::whereHas('roles', function($q) {
$q->whereHas('permissions', $function($q) {
$q->where('name', $value);
});
});
Run Code Online (Sandbox Code Playgroud)
结果是正确的,但如果我想通过更多条件搜索用户,我会给出其他权限值并使用orWhere
,它会响应所有拥有任何权限的用户,如何修复它?
$user = User::whereHas('roles', function($q) {
$q->whereHas('permissions', $function($q) {
$q->where('name', $value)->orWhere('name', $value2);
});
});
Run Code Online (Sandbox Code Playgroud) http://api.chan15.info/google-stackoverflow.html
这是我用来让用户通过谷歌JavaScript API登录的示例代码,它的工作原理,下一步是使用用户ID通过PHP登录本地服务器,但是使用JavaScript的用户ID是非常危险的,我想要的真正程序是:
但我不知道如何获得访问令牌.
php oauth youtube-javascript-api google-api-javascript-client
<?php
class Target
{
public function validate()
{
$this->getData();
return true;
}
public function getData()
{
return array();
}
}
Run Code Online (Sandbox Code Playgroud)
<?php
class TargetTest extends PHPUnit_Framework_TestCase
{
public function testValidate()
{
$mock = m::mock('Target');
$mock->shouldReceive('getData')
->once();
$expected = $this->exp->validate();
$this->assertTrue($expected);
}
}
Run Code Online (Sandbox Code Playgroud)
Mockery\Exception\InvalidCountException: Method getData() from Mockery_1_ExpWarning should be called
exactly 1 times but called 0 times.
我使用Mockery
作为模拟工具,示例总是关于如何模拟DI
,我想知道我可以模拟内部方法吗?
我有很多用户和角色结构
用户
ID
名称
角色
ID
名称
role_user
user_id
role_id
模型
user.php的
public function roles() {
return $this->belongsToMany('Role');
}
Run Code Online (Sandbox Code Playgroud)
Role.php
public function users() {
return $this->belongsToMany('User');
}
Run Code Online (Sandbox Code Playgroud)
有两个数据admins
和members
角色表,我想知道过滤用户哪个角色是管理员.
for (i = 1; i <= 6; i++) {
$.post("ajax.php", {param: i}, function (response) {
console.log(i);
});
}
Run Code Online (Sandbox Code Playgroud)
如何在$.post
完整函数中获得正确的i变量,我可以将变量传递给它吗?
php ×5
jquery ×2
laravel ×2
phpunit ×2
comparison ×1
google-api-javascript-client ×1
if-statement ×1
javascript ×1
laravel-4 ×1
mockery ×1
mysql ×1
oauth ×1
phpmd ×1
unit-testing ×1