如何在插入mongodb之前在模型中设置数据类型? (Laravel-mongodb的)

我从这里得到了参考: https://github.com/jenssegers/laravel-mongodb </ p>

我在mongodb中创建数据库和集合。 我不在mongodb中制作文档和专栏。 因为它会在插入数据时自动生成列和数据类型</ p>

我的控制器,您可以在下面看到:</ p>

  public function  create(array $ attributes)
{
...
$ result = User :: create($ attributes);
...
}
</ code> </ pre>

我的模型,你可以在下面看到:</ p>

 &lt;?php 
...
use Jenssegers \ Mongodb \ Eloquent \ Model as Eloquent;

use Jenssegers \ Mongodb \ Eloquent \ HybridRelations;

class用户扩展Eloquent
{
使用HybridRelations;

使用SoftDeletes;

protected $ connection ='mongodb';
public $ table ='users';
protected $ dates = ['deleted_at'];

public $ fillable = [
...
'name',
...
];

protected $ casts = [
...
'name'=&gt; 'string',
...
];
}
</ code> </ pre>

当我将数据插入mongodb时,mongodb上的数据类型将自动创建基于 输入</ p>

如何在模型中设置数据类型?</ p>

当我输入数据类型的数据时: string </ code > </ p>

我可以将它设置为数据类型:模型中的 integer </ code> </ p>

因此,形成的数据类型 在mongodb中是 integer </ code> </ p>
</ div>

展开原文

原文

I get reference from here : https://github.com/jenssegers/laravel-mongodb

I make database and collection in mongodb. I don't make document and column in mongodb. Because it will automatically make the column and data type when insert the data

My controller, you can see this below :

public function create(array $attributes)
{
    ...
    $result = User::create($attributes);
    ...
}

My model, you can see this below :

<?php
...
use Jenssegers\Mongodb\Eloquent\Model as Eloquent; 
use Jenssegers\Mongodb\Eloquent\HybridRelations;

class User extends Eloquent
{
    use HybridRelations;  
    use SoftDeletes;

    protected $connection = 'mongodb';
    public $table = 'users';
    protected $dates = ['deleted_at'];

    public $fillable = [
        ...
        'name',
        ...
    ];

    protected $casts = [
        ...
        'name' => 'string',
        ...
    ];
}

When I insert data into mongodb, data type on mongodb will be automatically created based on input

How to set the data type in the model?

When I have an data type of data input : string

I can set it up to data type : integer in my model

So, data type that is formed in mongodb is integer

1个回答



MongoDB支持两个可以直接嵌入MongoDB文档的构造函数,允许您指定整数数据类型。 在MongoDB文档中,您使用以下内容:</ p>

  myField:NumberLong(“20372”)
</ code> </ pre>

to 指定您希望“myField”包含64位有符号整数。 如果您只需要32位有符号整数,则使用:</ p>

  myField:NumberInt(“20372”); 
</ code> </ pre>

您必须解决的问题是,在将文档文本传递给MongoDB驱动程序之前,如何在PHP脚本中生成该文档文本。 另请注意,NumberLong()和NumberInt()构造函数采用 string </ em>值,因此如果您的PHP脚本接收数字数据作为字符串,则不必在PHP中将这些字符串转换为数字类型 :当您将包含NumberLong()和NumberInt()构造函数的MongoDB文档传递给必需的驱动程序函数/方法时,MongoDB会为您执行此操作。</ p>

当然,要了解这是怎么回事 工作,最好在启动主项目的全面编辑之前设置一个简单的测试,这样当你继续进行所述编辑时,你就知道要事先做出哪些改变。</ p>
</ div>

展开原文

原文

MongoDB supports two constructors that can be embedded in the MongoDB document directly, allowing you to specify integer data types. In the MongoDB document, you use the following:

myField: NumberLong("20372")

to specify that you want "myField" to contain a 64-bit signed integer. If you only want a 32-bit signed integer, then you use:

myField: NumberInt("20372");

What you have to work out, is how to generate that document text within your PHP script, before passing the document text to the MongoDB driver. Note also that the NumberLong() and NumberInt() constructors take string values, so if your PHP script receives numeric data as strings, you don't have to convert those strings to a numeric type in PHP first: MongoDB will do that for you, when you pass a MongoDB document containing NumberLong() and NumberInt() constructors to the requisite driver functions/methods.

Of course, to find out how this works, it's best to set up a simple test before launching into full scale editing of your main project, so that when you move on to said editing, you know what changes to make beforehand.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问