我试图用DataMapper选择随机数据集,但似乎没有这样的功能支持.
例如,我有一组数据:
+-------------------+
| ID | Name | Value |
+-------------------+
| 1 | T1 | 123 |
| 2 | T2 | 456 |
| 3 | T3 | 789 |
| 4 | T4 | 101 |
| ----------------- |
| N | Tn | value |
Run Code Online (Sandbox Code Playgroud)
可能有很多数据,超过10万行.
我需要将数据映射到对象:
class Item
include DataMapper::Resource
property :id, Serial
property :name, String
property :value, String
end
Run Code Online (Sandbox Code Playgroud)
所以,问题是:如何从表中选择随机数据?
SQL中的类似查询将是:
SELECT id, name, value FROM table ORDER BY RAND() LIMIT n;
Run Code Online (Sandbox Code Playgroud) 我不知道如何在Ruby或Rails项目中设置BerkelyDB数据库.
有没有人有任何配置经验,他们可以谈论?
也许使用ActiveRecord或Datamapper?
如何在Google AppEngine上的Jruby上的DataMapper中使用list/array作为属性?
我目前正在开发一个基于现有数据库的新应用程序,利用DataMapper进行数据访问.但是,它在处理外键时的约定不是数据库使用的.
例:
class Invoice
include DataMapper::Resource
storage_names[:default] = 'invoices'
property :id, Serial
# ... more properties ...
has n, :items
end
class Item
include DataMapper::Resource
storage_names[:default] = 'invoiceItems'
property :id, Serial
# ... more properties ...
belongs_to :invoice # this should use 'invoiceId' instead of 'invoice_id'
end
Run Code Online (Sandbox Code Playgroud)
有没有什么办法可以让DataMapper使用的外键是'invoiceId',而不是它目前尝试使用的'invoice_id'(如上面的评论所示)?我知道这可以通过添加正常字段来完成,:field => 'fieldName'但我找不到关联的方法.
我正在设计一个贷款发放系统,允许其用户创建贷款,根据贷款产品参数绘制贷款的还款计划.我也应该能够增加罚款,费用等.重新安排贷款应该是可能的.我还需要贷款计划来快速报告.
我有贷款表,贷款产品表,付款计划表和贷款历史表等.我无法理解如何设计这个架构以防止它变化太多.
我在ruby,rails3和datamapper中这样做.
我正在使用DataMapper(ruby gem)作为mysql数据库的ORM.(dm-core 1.1.0,do-mysql-adapter 1.1.0,do_mysql 0.10.6)
我正在编写一个包含两个表的应用程序:一段时间内磁盘使用情况的日志,以及一个"当前用法"表,其中包含具有"最新"磁盘使用情况的外键,以便于参考.DataMapper类是Quota和LatestQuota,带有一个简单的模式:
class Quota include DataMapper::Resource property :unique_id, Serial, :key => true property :percentage, Integer ... (more properties) end class LatestQuota include DataMapper::Resource belongs_to :quota, :key => true end
在我的代码中,我想找到LatestQuota表中与百分比高于95的配额相对应的所有条目.我正在使用以下datamapper查询:
quotas = LatestQuota.all(:quota => {:percentage.gte => threshold})
...later...
quotas.select{|q| some_boolean_function?(q)}
some_boolean_function是以DataMapper无法知道的方式过滤掉结果的东西,因此我需要调用ruby的select().
但它最终调用以下SQL查询(从DM的调试输出报告:)
SELECT `unique_id` FROM `quota` WHERE `percentage` >= 95
然后:
SELECT `quota_unique_id` FROM `latest_quota` WHERE `quota_unique_id` IN (52, 78, 82, 232, 313, 320…. all the unique id's from the above query...) …
我正在使用datamapper存储到数据库.迁移db后立即没有任何方法,因为没有值
<input type="text" name="seed" value="<%= @seed.value unless @seed.respond_to(value)? %>"
Run Code Online (Sandbox Code Playgroud) 我有一个SchoolDay代表上学日的课程:它可以告诉你日期,学期,学期,周和日.它可以生成像"Sem1 13A Fri"这样的字符串.要将这些对象存储在数据库中,我希望将它们序列化为字符串.
这是我的DataMapper自定义类型代码.我有点从dm-types中的代码中剔除了想法,因为(令人失望的是)没有用于创建自定义类型的真实文档.对不起,很久了.
module DataMapper
class Property
class SchoolDay < DataMapper::Property::String
#load_as ::SchoolRecord::DomainObjects::SchoolDay
# Commented out: the 'load_as' method is not found
def load(value)
# Take a string from the database and load it. We need a calendar!
val = case value
when ::String then calendar.schoolday(value)
when ::SR::DO::SchoolDay then value
else
# fail
end
end
def dump(value)
# Store a SchoolDay value into the database as a string.
case value
when SR::DO::SchoolDay
sd = value
"Sem#{sd.semester} #{sd.weekstr} …Run Code Online (Sandbox Code Playgroud) 我是学说2的新人.
为什么Doctrine 2没有基本的验证方法来验证所有值是否适合实体属性?
我的问题目标是更多地了解学说2如何工作以及为什么不说教义2中的错误.(主要是因为我是新的我错过了一些关于教义2设计方式的理解)
例:
<?php
// entities/User.php
/**
* @Entity @Table(name="users")
**/
class User
{
/**
* @Id @GeneratedValue @Column(type="integer")
* @var int
**/
protected $id;
/**
* @Column(type="string")
* @var string
**/
protected $name;
}
Run Code Online (Sandbox Code Playgroud)
使用build in validate的代码示例(不需要连接到db,只验证@Column(type ="integer"))在doctrine 2中不存在的基本函数:
$user=new User();
$user->setId('trtr');
$user->setName("goodname");
if($user->validate()){
echo 'ok';
}
else{
echo $user->validateError();
}
//output: id of User should be integer and not string
Run Code Online (Sandbox Code Playgroud)
谢谢
在我的项目中,我使用了领域驱动设计模式。我有各种数据映射器用于持久化我的模型对象。我的一些模型包含其他模型作为属性(例如,模型类 Book 包含一组模型类 Person 作为作者)。
Book
|->string title
|->Person[] authors
Run Code Online (Sandbox Code Playgroud)
每个模型都有一个对应的映射器(例如 Book_Mapper、Perspon_Mapper)。
在持久化模型对象时,一个映射器是否可以调用另一个映射器:
例如,当持久化 Book 对象时,我调用
Book_Mapper::Save(Book)
Run Code Online (Sandbox Code Playgroud)
其中调用
Person::Mapper(Person)
Run Code Online (Sandbox Code Playgroud)
对于这本书的每个作者?
datamapper ×10
ruby ×8
mysql ×2
activerecord ×1
berkeley-db ×1
doctrine ×1
doctrine-orm ×1
financial ×1
jruby ×1
orm ×1
sinatra ×1