我有一个很原始的问题:
我在Symfony App中创建了一个实体:
SRC /的appbundle /型号/条/ Article.php:
<?php
namespace AppBundle\Model\Article;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="articles")
*/
class Article {
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length="255", nullable=false)
*/
private $name;
}
Run Code Online (Sandbox Code Playgroud)
当我输入控制台时:
php app/console doctrine:generate:entities AppBundle
Run Code Online (Sandbox Code Playgroud)
它打印:
Bundle "AppBundle" does not contain any mapped entities.
Run Code Online (Sandbox Code Playgroud)
当我输入:
php app/console doctrine:generate:entities AppBundle/Model/Article/Article
Run Code Online (Sandbox Code Playgroud)
它打印:
Class "AppBundle\Model\Article\Article" is not a …Run Code Online (Sandbox Code Playgroud) 我有一个带有布尔字段的雄辩模型.数据库中的列是a tinyint(1),值正确存储为1.如果我在类之外的任何给定上下文中访问此值,我将获得正确的值:
$myModel = MyModel::first();
var_dump($myModel->visible); //outputs 1
Run Code Online (Sandbox Code Playgroud)
当我在模型类中的方法内访问它时虽然....
class MyModel {
public function isVisible(){
var_dump($this->visible);
// return $this->visible && $this->approved; // another true value
}
}
//on tinker
>> $myModel->isVisible();
array(0) {
}
>>
Run Code Online (Sandbox Code Playgroud)
我知道这听起来很疯狂,但我已经在这里呆了两个小时而且无法使它发挥作用.我错过了什么?
我正在尝试为一个全新的迷你应用程序编写一些单元测试。我通常会编写功能测试,因此这是我的工作,可以尝试通过模拟和存根以及所有与代码有关的事情正确地进行操作。
该模型如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class myModel extends Model
{
public static function getUser()
{
$user = \Auth::user();
return $user->adldapUser->cn[0];
}
}
Run Code Online (Sandbox Code Playgroud)
和测试:
class MyModelTest extends TestCase
{
public function testGetUser()
{
$mockResult = new StdClass();
$mockResult->adldapUser = new stdClass();
$mockResult->adldapUser->cn=array("test");
$auth = $this->getMock('Auth');
$auth
->method('user')
->will($this->returnValue($mockResult));
$this->assertEquals('test',\App\MyModel::getUser());
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行单元测试时,出现以下错误消息:
有1个错误:
1)MyModelTest :: testGetUser ErrorException:试图获取非对象的属性
/home/aidan/web/vagrant-web-dev/src/apps/orcid/app/MyModel.php:61 /home/aidan/web/vagrant-web-dev/src/apps/orcid/tests/MyModelTest.php :18
如果我发布$ user,则为NULL。
我在这里做错了什么?
因此,在我的SQL中,我的表仅包含三个字段-行ID,人员ID和状态ID,并且我想使用Laravel模型以及何时使用添加新行
$a = new AssignedStatus;
$a->person_id = $person_id;
$a->status_id = $status_id;
$a->save();
Run Code Online (Sandbox Code Playgroud)
我收到一条SQL错误,告诉您在此表中找不到列“ created_at”和“ updated_at”。是否有可能摆脱它们而仅插入行,还是使用DB::table连接更容易?
我创建了一个User模型和UserController
和我的模型 User.php是
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Aws\DynamoDb\DynamoDbClient;
class User extends \Aws\DynamoDb\DynamoDbClient
{
protected $table = 'users';
protected $fillable = array('email');
protected $primaryKey = 'user_id';
}
Run Code Online (Sandbox Code Playgroud)
当我在UserController中创建一个对象时
public function postSignUp(Request $request)
{
$user = new User();
Run Code Online (Sandbox Code Playgroud)
它说$ user = new User();(line30)有错误
传递给Aws\AwsClient :: __ construct()的参数1必须是类型数组,没有给定,在第30行调用C:....\laravel\app\Http\Controllers\UserController.php并定义
谢谢!!
我试图通过使用"使用"给了别名,模型类的名称使用在laravel雄辩的方法相关模型的类名.例如,我使用过UserProfile模型类:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\UserProfile;
Run Code Online (Sandbox Code Playgroud)
而现在我正在以雄辩的方式使用它,如下所示:
public function profileDetails() {
return $this->hasOne('UserProfile', 'user_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)
但这会引发一个错误.Class 'UserProfile' not found
如果我直接在这个雄辩的第一个参数中传递相关模型的名称和路径,那么它的工作正常
public function profileDetails() {
return $this->hasOne('App\Models\UserProfile', 'user_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么它没有合作 use
我似乎无法更新我的用户和学校表,但现在能够更新爱好表.
继续收到错误:implode():更新数据时传递的参数无效 - >链接回之前的问题
控制器:
//update for user
public function edit($id){
$object = user::find($id);
return view('edit', compact('object'));
}
public function update(Request $request, $id){
$object = user::find($id);
$object->Name = $request->input('Name');
$object->update();
return redirect('/home');
}
//update for Schools table
public function edit1($id){
$object2 = school::find($id);
return view('edit1', compact('object2'));
}
public function update1(Request $request, $id){
$object2 = school::find($id);
$test = array();
$test['School'] = implode(' , ', $request->School);
$test['SDate'] = implode(' , ', $request->SDate);
$test['EDate'] = implode(' , ', $request->EDate);
$object2->update($test);
return redirect('/home');
} …Run Code Online (Sandbox Code Playgroud) 我有一个模型,其中包含一个@property当我做类似
vals = MyModel.objects.values()
S / O帖子之类的事情时要包含
的模型,建议这样做的get_queryset方法是通过覆盖manager类的方法,并增加对的调用annotate(xtra_field=...)。
问题是我找不到如何使用模型属性执行此操作的示例。我已经尝试过类似的东西
super(SuperClass, self).get_queryset().annotate(xtra_fld=self.model.xtra_prop)
及其众多变体,但都无济于事。
我认为问题是注释参数的RHS应该是什么?
(而且,是的,我知道还有其他更笨拙的方法可以做到这一点,例如遍历values()返回的收益并随便添加属性。对我来说,这不是一个非常优雅(甚至是Django)的解决方案。
我正在将Rails 5.1应用程序迁移到Rails 5.2.1。在我的模型中,我使用回调在创建或更新模型后创建活动日志。不幸的是todo.name,todo.name_was并且始终不变-当前值。这适用于每个属性和每个模型。还changed?返回false。
我想念什么吗?
非常感谢您的帮助!
我遵循数据结构,
产品
产品审核
关系是一对多,即一个产品有很多评论.
我想要的是加载所有经过验证评论的产品(即状态为真的评论)
我试过跟着,
$products = Product::with('reviews')->get();
Run Code Online (Sandbox Code Playgroud)
但是当我迭代$products访问$ product-> reviews等评论时,会显示所有评论(甚至是状态错误评论).
任何建议都表示赞赏.
model ×10
php ×6
laravel ×5
eloquent ×2
laravel-5 ×2
relationship ×2
annotate ×1
attributes ×1
controller ×1
django ×1
doctrine ×1
entity ×1
laravel-5.1 ×1
mocking ×1
properties ×1
schema ×1
symfony ×1
this ×1
typing ×1
unit-testing ×1