小编Joh*_*ohn的帖子

Laravel 4 Eloquent ORM通过动态属性访问一对一关系

我试图在'Users'表和'User_profiles'表之间建立一个非常简单的关系模型.每个用户都有一个user_profile,所以它是一对一的简单.根据发现的文档@ http://four.laravel.com/docs/eloquent#one-to-one我已将以下功能添加到我的用户模型中:

public function user_profile()
{
    return $this->hasOne('User_profile');
}
Run Code Online (Sandbox Code Playgroud)

这是我的User_profile模型中定义的关系:

public function user()
{
    return $this->belongsTo('User');
}
Run Code Online (Sandbox Code Playgroud)

我试图从控制器访问像这样:

    // Get current user
    $user = User::find(Auth::user()->id);
    $profile = $user->user_profile;

    print_r($user);
    print_r($profile);

    echo "User's name is: " . $user->user_profile->first_name . ' ' . $user->user_profile->last_name;
Run Code Online (Sandbox Code Playgroud)

不幸的是,打印$ user打印出用户模型字段就好了,但没有显示任何关系的痕迹; $ profile是空的.'relations'数组也是空的,我猜应该填充它.

我正在尝试使用此处建议的"动态属性" http://four.laravel.com/docs/eloquent#dynamic-properties

否则如果我去:

echo "User's name is: " . $user->user_profile()->first()->first_name . ' ' . $user->user_profile()->first()->last_name;
Run Code Online (Sandbox Code Playgroud)

它有效......但我真的不喜欢这样做.

有什么建议?

laravel eloquent laravel-4

6
推荐指数
1
解决办法
3532
查看次数

定义了一个重复的"entityFramework"部分 - EntityFramework6升级

我最近在我的WebAPI项目中更新了从v5到v6的EntityFramework.不幸的是,似乎某个地方,不知何故,某个配置中存在一个设置为v5.0的引用.我似乎无法找到它; 我将在下面发布我的web.config.

显示的错误是:

定义了重复的"entityFramework"部分.

我找不到重复的引用,但是如果我删除了单个引用,那么我会收到一条错误说:

{"Message":"发生错误.","ExceptionMessage":"无法加载文件或程序集'EntityFramework,Version = 5.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'或其依赖项之一.找到的程序集manifest定义与程序集引用不匹配.(HRESULT异常:0x80131040)","ExceptionType":"System.IO.FileLoadException","StackTrace":"at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host,String typeString,布尔值throwOnError)\ r \n在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord,FactoryRecord factoryRecord)\ r \n在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.在System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey,Boolean&isRootDeclaredHere)的System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord)\ r \n的InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord,FactoryRecord factoryRecord)\ r \n

这是我的web.config:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      </configSections>
      <connectionStrings></connectionStrings>
      <appSettings>
        <add key="webpages:Version" value="2.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="PreserveLoginUrl" value="true" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
      <system.web> …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

eloquent ×1

entity-framework ×1

laravel ×1

laravel-4 ×1