【问题标题】:Whats the best way to create an array of objects inside a class as attribute in PHP?在类中创建对象数组作为 PHP 中的属性的最佳方法是什么?
【发布时间】:2020-11-27 03:03:33
【问题描述】:

所以我很难找出最好的方法和正确的方法来做到这一点,所以如果有人可以在这里帮助我,我会发布这个问题,因为我正在用 PHP 做一个 Laravel 项目。

假设我们正在为书籍构建一个应用程序,每本书都有几页,有些有 20 页,有些可能有 700 页,如果我们要为这些书籍正确创建一个或多个类,我们该怎么做?

下面我有几种方法可以解决,但我不知道哪种方法可行,和/或哪种方法最好。

选项 1:

<?php
namespace Translation/Logic;

public class Book {
  // atributes
  public $title;
  public $year;
  public $author;
  public $description;
  public $date;
  public $language = "English";

  // as an atribute maybe we need to have an array of pages here? like
  // public $pages = Pages[]??
}

public class Pages {
   public number;
   public orientation;
   public direction;
   public page;
   public chapter;
   public content;

}

选项 2:

<?php
namespace Translation/Logic;

abstract class Book {
  // atributes
  public $title;
  public $year;
  public $author;
  public $description;
  public $date;
  public $language = "English";

  // as an attributes maybe we need to have an array of pages here? 
}

public class Pages extends Book {
   public number;
   public orientation;
   public direction;
   public page;
   public chapter;
   public content;

}
// Then create an array of Pages here, but then we have all the attributes of the books in every page, which don't really make sense?

我很确定我在这里做错了,有一种更聪明的方法可以做到这一点,但在这里只需要一点帮助。

如果它是我的解决方案之一,应该完成我的代码的代码是什么,除了我的之外还有其他替代方法,每种方法的优缺点是什么。

如果不是而且我错了,那么定义这些类的正确方法是什么,为什么?

感谢您花时间回答我:)

【问题讨论】:

  • 如果你使用 Laravel,请使用模型。您将有一个Book 模型,一个Page 模型,并且数据库将支持它们之间的一对多结构(其中一本书可以有一个或多个页面)。所有这些属性(如页面、方向、章节、内容等)都是数据库列。
  • 是的,我会使用模型,但是如果我想将模型中的一本书的数据加载到类对象中,进行操作等。能够做到这一点,这就是问题所在。
  • 这将通过模型完成; public function pages(),您所要做的就是调用$book-&gt;pages,您将拥有特定书籍的页面模型集合:laravel.com/docs/7.x/eloquent-relationships#one-to-many。默认情况下,当您查询数据库时,会加载 Book 的所有属性。

标签: php laravel class


【解决方案1】:

正如 Tim Lewis 在 cmets 中对他的问题所说,最好的方法是使用 Eloquent 模型。

让我给你一个简单的例子,说明如何做到这一点的最佳方式。

这将是他的图书课

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['title', 'year', 'author', 'description', 'date', 'language'];

    /**
    * Get the pages of this book.
    *
    * @return \Illuminate\Database\Eloquent\Relations\HasMany
    */
    public function pages()
    {
        return $this->hasMany('App\Page');
    }

}

这将是他的页面类

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['number', 'orientation', 'direction', 'page', 'date', 'chapter', 'content'];

    /**
    * Get the book of this page.
    *
    * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
    */
    public function book()
    {
        return $this->belongsTo('App\Book');
    }

}

因此,如果您想获取所有包含所有页面的书籍,则必须在这样的控制器中进行。

$books = Book::with('pages')->get();

如果您只想获得一本包含您的页面的书

$book = Book::with('pages')->first();

【讨论】:

  • 不错的总结!你快到了,但你描述Book::with('pages')-&gt;get()Book::with('pages')-&gt;first() 之间区别的方式不太正确。区别在于Book::with('pages')-&gt;get() 将返回“数据库中的所有书籍及其所有页面”,而Book::with('pages')-&gt;first() 将返回“数据库中的第一本书及其所有页面”。 IE。都加载所有页面:)
  • 是的,这正是我要解释的内容,很明显,如果您不想从数据库中获取第一个,只需在 first 之前执行 where 即可。这只是一个简单的例子。
  • 谢谢大家,事实上这似乎可行,Eloquent models 是这里的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2021-10-25
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
相关资源
最近更新 更多