对于我的用户的 CRUD,我正在使用sequelize-mock和jest编写单元测试。模型模拟对我有用,但我的问题是关于模拟模型findAll(),并且findOne()在我的用户测试套件中调用它们都返回我在模拟模型中定义的排队结果。我期望如果我调用findAll(),我应该获取在排队结果中定义的所有数据,如果我调用,findOne()我应该只根据我传递的 id 从排队结果中获取一个数据。设置模拟模型autoQueryFallback: false对我不起作用。
这是一个代码片段:
在我的controllers/users.js
module.exports = {
async list (req, res) {
Users.findAndCountAll({
limit: 10,
offset: 0
}).then(data => {
console.log('Users >>> ', data);
});
},
async view (req, res) {
Users.findOne({
where: { id: req.params.id }
}).then(data => {
console.log('User Data by ID >>> ', data);
});
}
};
Run Code Online (Sandbox Code Playgroud)
为了模拟我的用户模型,我必须创建一个__mocks__与模型相邻的目录(这就是 Jest 的工作原理)。所以,我的模型中有这个目录结构
> models/users.js
> models/__mocks__/users.js
Run Code Online (Sandbox Code Playgroud)
const Users = dbMock.define('users', …Run Code Online (Sandbox Code Playgroud) 我坚持在产品系列中获取产品网址.我有这些代码来获得针对特定产品的产品系列.
$_productCollection = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->getCollection();
$_productCollection->addAttributeToFilter('feat_enabled', 1);
$_productCollection->setVisibility(array(2,3,4));
$_productCollection->addUrlRewrite();
$_productCollection->groupByAttribute('entity_id');
$_productCollection->load();
foreach($_productCollection as $_product){
$_product->load($_product->getId());
if($_product->getIsSalable()){
$_name = ltrim(rtrim($_product->getName()));
$_url = $_product->getProductUrl();
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么,如果在每个循环中回显$ _url,则返回的url缺乏.它没有类别名称和子目录名称.正确的网址应该是:
的index.php /类别/子/ itemname.html
但目前它只会返回
index.php/itemname.html
Run Code Online (Sandbox Code Playgroud)
它返回纠正除url之外的数据.如果我将项目分配到类别和子类别,我有管理员的双重检查,我确认我分配了它.
Upon further research, I have found the code below that is very close to what i need.
$_categories = $_product->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($_categories[0]);
$_url = Mage::getUrl($_category->getUrlPath()).basename($_product->getProductUrl());
Run Code Online (Sandbox Code Playgroud)
这里的问题,$ _url值变成了这样
的index.php /类别/ subcategory.html/itemname.html
子类别名称中包含.html.我的网址中不需要.html,因此点击时该项目会被重定向到正确的页面.
有什么想法解决这个问题?
在CI中,你如何将模型相互关联?我现在有四个模型用户,UsersDepartment,UsersToDepartment,UserStatus和我需要加入这四个模型才能获取所有数据.
我在我的控制器中有这个代码从用户表中选择所有用户数据:
function view($user_id){
$data['user'] = $this->User_model->get_by_id($user_id)->row();
}
Run Code Online (Sandbox Code Playgroud)
保存在Users表中的user_status只是status_id,因此我需要连接到UserStatus表以获取users_status_id的等效名称.我需要知道用户所属的组列表.所以我需要根据Users.userid从UsersToDepartment表中获取它.然后在UsersDepartment表中获取等效的组名.请参阅我的图表以进一步解释.

我知道在本机PHP中,这可以通过使用join来完成.如何在CI中完成?
我知道yii,你可以这样做
$posts=Post::model()->with(
'author.profile',
'author.posts',
'categories')->findAll();
Run Code Online (Sandbox Code Playgroud)
CI也可以吗?
我知道这\s是用于匹配字符串中任何空格的模式.现在,我的问题是在字符串的开头或结尾检查空格的确切正则表达式是什么?
所以,如果我有一个字符串"hello world",那么空格就在起始处,所以它会返回true.如果字符串是"hello world",它也将返回true.白色空间位于字符串的开头和结尾.
我不需要删除空格,但我只需要检查数据库中的数据是否在开头或结尾都有空格.
这是我的SQL查询,但这不起作用.这只会获取最后有空格的数据:
select * from `data_table`
where `data_content` regexp '[ \f\t\v]$';
Run Code Online (Sandbox Code Playgroud)
上面的查询给出了7个结果,因为只有7个记录在末尾有空格.我需要获取3条记录,这些记录在字符串的开头有空格.
我试过运行这个SQL查询:
select * from `data_table`
where `data_content` regexp '^\s' || `data_content` regexp'\s$';
Run Code Online (Sandbox Code Playgroud)
它给了我87个结果.我通过PHPMYADMIN逐个检查结果,我得到的结果是那些没有空格的数据.
这套RegEx有什么问题/^[\p{L}\p{N}]+/u.当我的高级输入%openminded时正则表达式返回false.我需要它接受这种格式
%openminded
100%openminded
openminded 100%
我需要在表达式中添加什么?因此,即使用户%首先输入或任何特殊字符,它也会接受输入.
regex ×2
codeigniter ×1
jestjs ×1
magento ×1
models ×1
mysql ×1
node.js ×1
sequelize.js ×1
unicode ×1
unit-testing ×1
whitespace ×1