我可以找到一些有关此问题的讨论,但没有明确的解决方案.这里有两个链接,虽然我将在这里讨论我自己的问题.
对于已经熟悉Laravel多态关系的人来说,这是对我的问题的简单解释.
当使用$ morphClass时,在尝试查找多态关系的所有者时,将$ morphClass的内容作为变体"type"保存在数据库中用于类名.这会导致错误,因为$ morphClass的整个点是它不是类的完全命名空间名称.
如何定义多态关系应该使用的类名?
这是一个更详细的解释,准确地解释了我正在尝试做什么以及为什么我试图用例子来做.
在Laravel中使用多态关系时,无论保存为什么,数据库中的"morph_type"类型都被认为是类.
所以在这个例子中:
class Photo extends Eloquent {
public function imageable()
{
return $this->morphTo();
}
}
class Staff extends Eloquent {
public function photos()
{
return $this->morphOne('Photo', 'imageable');
}
}
class Order extends Eloquent {
public function photos()
{
return $this->morphOne('Photo', 'imageable');
}
}
Run Code Online (Sandbox Code Playgroud)
数据库看起来像这样:
staff
- id - integer
- name - string
orders
- id - integer
- price - integer
photos
- id - integer
- …Run Code Online (Sandbox Code Playgroud) 我使用的是Laravel框架,这个问题与在Laravel中使用Eloquent直接相关.
我正在尝试制作一个可以在多个不同表中使用的Eloquent模型.这样做的原因是我有多个表基本相同但每年都不同,但我不想重复代码来访问这些不同的表.
我当然可以有一个带有年份列的大表,但是每年有超过350,000行和多年处理我决定将它们分成多个表更好,而不是4个带有额外'where'的大表在每个请求上.
所以我想要做的是为每个类创建一个类,并在Repository类中执行类似的操作:
public static function getTeam($year, $team_id)
{
$team = new Team;
$team->setYear($year);
return $team->find($team_id);
}
Run Code Online (Sandbox Code Playgroud)
我在Laravel论坛上使用了这个讨论让我开始:http://laravel.io/forum/08-01-2014-defining-models-in-runtime
到目前为止我有这个:
class Team extends \Illuminate\Database\Eloquent\Model {
protected static $year;
public function setYear($year)
{
static::$year= $year;
}
public function getTable()
{
if(static::$year)
{
//Taken from https://github.com/laravel/framework/blob/4.2/src/Illuminate/Database/Eloquent/Model.php#L1875
$tableName = str_replace('\\', '', snake_case(str_plural(class_basename($this))));
return 'gamedata_'.static::$year.'_'.$tableName;
}
return Parent::getTable();
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但我担心它不能以正确的方式工作.
因为我使用静态关键字,所以属性$ year保留在类中而不是每个单独的对象中,因此每当我创建一个新对象时,它仍然保持$ year属性,该属性基于上次在另一个对象中设置的属性.我宁愿$ year与单个对象相关联,每次创建对象时都需要设置.
现在我试图追踪Laravel创建Eloquent模型的方式,但真的很难找到合适的地方来做到这一点.
例如,如果我将其更改为:
class Team extends \Illuminate\Database\Eloquent\Model {
public $year;
public function setYear($year)
{ …Run Code Online (Sandbox Code Playgroud) (在我开始之前我应该说是的,我已经完成了所有的愚蠢检查,是的,链接在我的历史中并且已被访问过等)
我使用的是Chrome版本6.0.472.63,但重要的是它适用于所有浏览器.
它适用于Firefox,IE和Opera.
基本上我所要做的就是在链接被访问后更改链接的背景图像.
我做了很多试验和错误测试,所以请耐心等待多个例子.
这就是我原来的样子
.forum_box .title a {
background-image:url(../images/f_unread.png);
background-position:10px center;
background-repeat:no-repeat;
background-color:transparent;
color:#2D4054;
font-size:14px;
padding:10px 12px 10px 44px;
text-decoration:none;
display:block;
font-weight:bold;
}
.forum_box .title a:visited {
background-image:url(../images/f_read.png);
}
适用于Chrome以外的所有浏览器.接下来我试着把它变成一种颜色而不是图像.
.forum_box .title a:visited {
background-color:red;
}
同样,我将链接更改为#fff而不是透明,访问链接更改为红色,所以显然bg颜色只有在为父级设置bg颜色时才有效.
.forum_box .title a {
background-image:url(../images/f_unread.png);
background-position:10px center;
background-repeat:no-repeat;
background-color:#fff;
color:#2D4054;
font-size:14px;
padding:10px 12px 10px 44px;
text-decoration:none;
display:block;
font-weight:bold;
}
.forum_box .title a:visited {
background-color:red;
}
但它仍然无法解决我的图像问题.因此,在最后的一次尝试中,我尝试了这一点,希望Chrome出于某种原因只有当两者中存在相同属性时才能工作.
.forum_box .title a {
background:#fff url(../images/f_unread.png) no-repeat 10px center;
color:#2D4054;
font-size:14px;
padding:10px 12px 10px 44px; … 编辑:找出我出错的地方并在最后找到答案
我正在尝试创建一个Laravel命令,我可以看到它在Laravel 3中的"任务"中发生了相当大的变化.但是我似乎无法让它运行.这些是我采取的步骤:
php artisan命令:make import
返回
命令创建成功
然后创建命令目录中的文件并稍微修改以返回"Hello World",如下所示:
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class Import extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'command:import';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return void …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用laravel查询构建器来复制连接:
LEFT JOIN content_userdata
ON content_id = content.id
AND user_id = $user_id
Run Code Online (Sandbox Code Playgroud)
我发现我可以使用扩展Eloquent的模型中的以下函数来执行其他"ons"
public function scopeJoinUserData($query, $user_id)
{
return $query->leftJoin('content_userdata', function($join)
{
$join->on('content_userdata_content_id', '=', 'content.content_id')->on('content_userdata_user_id', '=', 10);
});
}
Run Code Online (Sandbox Code Playgroud)
但这会产生两个问题.首先,我无法将$ user_id变量放入函数中,其次,即使我将其硬编码用于测试目的,如上所述(对于"10"),Laravel将其包含在`意味着它应该被解释为列名时不是这样的:
left join `content_userdata`
on `content_id` = `content`.`id`
and `user_id` = `10`
Run Code Online (Sandbox Code Playgroud)
所以现在我有两个问题.
我为什么要这样做? 我意识到一个回应可能是把它放在哪里.但是我试图这样做,因为连接可能不一定返回任何结果(因此左连接),因为content_userdata表包含诸如用户对一段内容的评级之类的内容.如果我使用where,那么content_userdata表中没有任何内容的结果将不会被返回,就好像我可以将它放入连接中那样,由于左连接,它们将被返回.
无论如何在Laravel中实现这一目标,如果没有替代方案,显然完全改变了放弃Laravel是最重要的,但我能想到的唯一选择是在单独的查询中获取用户数据.
使用Laravel任务调度程序我在Kernel.php中创建了许多任务
例如:
$schedule->command('some:command')
->daily();
$schedule->command('another:command')
->daily();
Run Code Online (Sandbox Code Playgroud)
我想显示预定命令的列表和频率(以及上一个/下一个运行时间,我可以使用之前/之后的函数记录自己).
但是我陷入了第一道障碍.我不知道该怎么做是获取已在Kernel.php中定义的计划任务的数组
// Example function needs to be created
$tasks = getAllScheduledTasks();
@foreach($tasks as $task)
<tr>
<td>{{ $task->name }}</td>
<td>{{ $task->description }}</td>
</tr>
@endforeach
Run Code Online (Sandbox Code Playgroud)
简化问题:如何在Laravel中获取一系列计划任务?
在Firefox 7.0.1中,我有两个复选框和许多其他输入.
当我通过jQuery添加另一个输入时,Firefox无法正确记住选择了哪些无线电输入.
例如,如果我选择第一个单选按钮然后刷新页面,则选择第二个单选按钮而不是第一个,如果我再次刷新,则不会选择单选按钮.
您应该能够将以下代码复制并粘贴到新文件中以进行自我测试:
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('select').after('<input class="select" type="text" name="new_text_input" />');
});
</script>
<title>Pretty jQuery Form</title>
</head>
<body>
<form>
<fieldset>
<label>Select Box</label>
<select name="my_select">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
</fieldset>
<fieldset>
<label>Text Input</label>
<input class="text" id="text_input" name="input" type="text" />
</fieldset>
<fieldset>
<label>Text Area</label>
<textarea></textarea>
</fieldset>
<fieldset>
<label>Radio</label>
<input value="1" name="radio" id="radio1" type="radio" /> <label for="radio1">Radio 1</label>
<input value="2" name="radio" id="radio2" type="radio" /> <label for="radio2">Radio …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用twitter bootstrap的affix插件,但我无法弄清楚如何在父容器中约束它,我创建了以下示例来显示我的问题:
问题演示:http://www.codeply.com/go/DvcRXkeFZa
<div class="container">
<div class="row">
<div class="col-md-3 column">
<ul id="sidebar" class="well nav nav-stacked" data-spy="affix" data-offset-top="130">
<li><a href="#software"><b>Software</b></a></li>
<li><a href="#features">Features</a></li>
<li><a href="#benefits">Benefits</a></li>
<li><a href="#costs">Costs</a></li>
</ul>
</div>
<div class="col-md-9 column">
<h1>Blah, blah, blah</h1>
<hr>
<a class="anchor3" id="top" name="software"></a>
<p>
content here that scrolls...
</p>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的那样,当它滚动时,它会溢出侧边栏,当它到达底部时也不会粘贴到页面底部.
我正在使用Laravel 4,并通过作曲家加载了hybridauth,并使其在Facebook和Twitter上运行良好.现在我正在尝试使用Steam,它被列为一个额外的提供程序,但是我不断收到以下错误:
require_once(vendor/hybridauth/hybridauth/hybridauth/Hybrid/Providers/Steam.php)[function.require-once]:无法打开流:没有这样的文件或目录
显然它正在寻找错误的地方,实际的类位于这个位置:
供应商/ hybridauth/hybridauth /附加提供商/ hybridauth蒸汽/提供者/ Steam.php
有很少的文档,我可以找到关于这一点,我只能猜测是hybridauth的作者只提供这些额外提供选装,并希望你之类的位置移动到合适的位置,但与作曲家,这不是做事情的方式,并且在我运行作曲家更新的任何时候都会导致问题.
我找不到任何通过谷歌遇到类似问题的人,这看起来很奇怪,所以我担心我会发现很明显.有没有办法使用其他提供程序,让他们自动加载,同时使用hybridauth与Composer?
我能想到的唯一解决方案是在尝试自动加载之前手动包含正确的文件.我不介意这样做,但我假设必须有一个正确的方法来做这个,否则使用Composer与Hybridauth是相当无用的.
使用试验和错误我发现,从以下查询中删除连接时,它运行速度提高了大约30倍.有人可以解释为什么会这样,并且是否可以优化查询以包含额外的连接而不会影响性能.
这是解释的屏幕截图,显示该索引未用于uesr_groups表.

这是原始查询:
SELECT `comments`.`comment_id`, `comments`.`comment_html`, `comments`.`comment_time_added`, `comments`.`comment_has_attachments`, `users`.`user_name`, `users`.`user_id`, `users`.`user_comments_count`, `users`.`user_time_registered`, `users`.`user_time_last_active`, `user_profile`.`user_avatar`, `user_profile`.`user_signature_html`, `user_groups`.`user_group_icon`, `user_groups`.`user_group_name`
FROM (`comments`)
INNER JOIN `users` ON `comments`.`comment_user_id` = `users`.`user_id`
INNER JOIN `user_profile` ON `users`.`user_id` = `user_profile`.`user_id`
INNER JOIN `user_groups` ON `users`.`user_group_id` = `user_groups`.`user_group_id`
WHERE `comments`.`comment_enabled` = 1
AND `comments`.`comment_content_id` = 12
ORDER BY `comments`.`comment_time_added` ASC
LIMIT 20
Run Code Online (Sandbox Code Playgroud)
如果我删除"user_groups"连接,则查询运行速度提高30倍,如上所述.
SELECT `comments`.`comment_id`, `comments`.`comment_html`, `comments`.`comment_time_added`, `comments`.`comment_has_attachments`, `users`.`user_name`, `users`.`user_id`, `users`.`user_comments_count`, `users`.`user_time_registered`, `users`.`user_time_last_active`, `user_profile`.`user_avatar`, `user_profile`.`user_signature_html`
FROM (`comments`)
INNER JOIN `users` ON `comments`.`comment_user_id` = `users`.`user_id`
INNER JOIN `user_profile` …Run Code Online (Sandbox Code Playgroud) laravel ×6
laravel-4 ×5
eloquent ×3
join ×2
php ×2
affix ×1
command-line ×1
composer-php ×1
css ×1
css-position ×1
firefox ×1
hybridauth ×1
inner-join ×1
laravel-5 ×1
left-join ×1
mysql ×1
polymorphism ×1
radio-button ×1
sticky ×1
visited ×1