这是我的例子
<?php
/*
Template Name: SQL Templatess
*/
get_header();
global $wpdb;
/**
* Function is used to select fields from BarZap and ICMOrders database.
*/
function selectDatas($Fields) {
global $wpdb;
$slectSQL = "SELECT $Fields
FROM barZapp.Venue AS venue
INNER JOIN ICMOrders.Customer AS customer ON customer.ID = venue.CustomerId
INNER JOIN ICMOrders.Order AS orderP ON customer.ID = orderP.CustomerID
WHERE orderP.OrderType='BarZapp' GROUP BY orderP.CustomerID";
return $slectSQL;
}
$insert4User = selectDatas("customer.ID,customer.Name,venue.password,customer.EmailAddress,venue.creationdate,venue.country,
venue.state, venue.city, venue.zip, venue.contactphone");
$selectQuery = $wpdb->get_results($insert4User, OBJECT );
foreach ($selectQuery as $keyValues) {
$result = $wpdb->insert(
$wpdb->prefix . 'users',
array(
'spam' => 0,
'user_status' => 0,
'deleted' => 0,
'old_user_id' =>$keyValues->ID,
'user_login' =>$keyValues->Name,
'user_pass' =>$keyValues->password,
'user_nicename' =>$keyValues->Name,
'user_email' =>$keyValues->EmailAddress,
'display_name' =>$keyValues->Name,
'user_registered' =>$keyValues->creationdate,
)
);
$user_id = $wpdb->insert_id; ## Get the last inserted ID of user's
$forUserrole = new WP_User( $user_id );
add_user_meta( $user_id, 'nickname', $keyValues->Name);
add_user_meta( $user_id, 'first_name', $keyValues->Name);
add_user_meta( $user_id, 'rich_editing', true);
add_user_meta( $user_id, 'admin_color', 'fresh');
add_user_meta( $user_id, 'use_ssl', 0);
add_user_meta( $user_id, 'show_admin_bar_front', 0);
add_user_meta( $user_id, 'ic_capabilities', $forUserrole->set_role('Multi'));
add_user_meta( $user_id, 'ic_user_level', 0);
add_user_meta( $user_id, 'billing_first_name', $keyValues->Name);
add_user_meta( $user_id, 'billing_email',$keyValues->EmailAddress);
add_user_meta( $user_id, 'billing_phone', $keyValues->contactphone);
add_user_meta( $user_id, 'billing_state', $keyValues->state);
add_user_meta( $user_id, 'billing_city', $keyValues->city);
add_user_meta( $user_id, 'billing_zip', $keyValues->zip);
add_user_meta( $user_id, 'billing_country', $keyValues->country);
}
get_footer();
现在我可以将此class User < ActiveRecord::Base
validates_with EmailValidator
end
class EmailValidator < ActiveModel::Validator
def validate(record)
if record != someregex
record.errors.add(:email, 'invalid email')
end
end
end
用于任何模型。但我的要求是验证其特定模型的独特性和存在性。
如果我能做到这一点,我可以使用此EmailValidator
进行任何模型电子邮件验证,其功能是唯一的,存在。
然后我可以实现更多可重用的Validator。
答案 0 :(得分:0)
您可以将验证程序重写为EachValidator
:
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if attribute !~ someregex
record.errors.add attribute, (options[:message] || 'invalid email')
end
end
end
然后在你的模型中:
validates :email, email: true, uniqueness: true, presence: true
希望有所帮助。