如何在Symfony 3 Doctrine中查找查询的所有位置

时间:2016-07-25 08:27:03

标签: php sqlite symfony doctrine-orm doctrine

我的表名为post,列string名为status,数据为publishedpending。现在我想用EntityRepository类自定义方法从此表中获取所有行,或者只修改现有方法。

我试过这个:

public function queryLatestPublished()
{
  return $this->getEntityManager()
        ->createQuery('
            SELECT p
            FROM AppBundle:Post
            WHERE p.staus == :st
            ORDER BY p.created DESC
        ')
        ->setParameter('now', new \DateTime())
        ->setParameter('st', 'published')
    ;
}

但查询不会返回任何内容。

2 个答案:

答案 0 :(得分:0)

您的代码中存在一些错误/问题

  • 提供的参数数量与预期的数量
  • 不匹配
  • 你没有调用getResult(或任何其他类似执行的方法),所以你实际上返回了Query对象
  • 我会更改方法的命名,因为它具有误导性

我也会改变

  • 您可能希望以某种方式限制结果数量
  • 如果您在实体存储库内(在本例中为Post),则可以省略使用selectfrom使用/** * @param int $limit * @param int $offset * * @return Post[] */ public function findNewestPublished($limit = 10, $offset = 0) { return $this->createQueryBuilder('p') ->where('p.status = :status') ->setParameter('status', 'published') ->addOrderBy('p.createdAt', 'desc') ->getQuery() ->setFirstResult($offset) ->setMaxResults($limit) ->getResult(); } public async Task<Member> AddOrUpdate(DownloadFormModel model) { IMailChimpManager mailChimpManager = new MailChimpManager(ConfigurationManager.AppSettings["mailChimpKey"]); Member member = new Member {EmailAddress = model.Email, Status = Status.Subscribed}; member.MergeFields.Add("NAME", model.Name); return (await mailChimpManager.Members.AddOrUpdateAsync(ConfigurationManager.AppSettings["mailChimpDownloadListID"], member)); }

我的主张:

  public class BuyerDetailFragment extends Fragment {
  private TextView TextView;
  public static TabLayout tabLayout;
  public static ViewPager viewPager;
  public static int int_items = 2 ;
  private Button request;
  public BuyerDetailFragment() {
    // Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_buyer_detail,   container, false);
    ((MainActivity) getActivity())
            .setActionBarTitle("مشخصات");
    TextView = (TextView)rootView.findViewById(R.id.textView21);
    request=(Button)rootView.findViewById(R.id.button3);
    request.setBackgroundColor(Color.parseColor("#FF5896E2"));
    request.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Create custom dialog object
            final Dialog dialog = new Dialog(getActivity());
            // Include dialog.xml file
            dialog.setContentView(R.layout.request_dialog);
            // Set dialog title
            dialog.setTitle("درخواست بازدید ملک");


            // if decline button is clicked, close the custom dialog
            declineButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    dialog.dismiss();
                }
            });
            Button acceptButton = (Button) dialog.findViewById(R.id.button6);
            // if decline button is clicked, close the custom dialog
            acceptButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    Toast.makeText(getActivity(),"درخواست فرستاده شد",Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                }
            });
        }
    });
    String content = getString(R.string.about_house);
    Spanned html = Html.fromHtml(content);
    TextView.setText(html);
    // Inflate the layout for this fragment
    tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
    viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);


    /**
     *Set an Apater for the View Pager
     */
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

    /**
     * Now , this is a workaround ,
     * The setupWithViewPager dose't works without the runnable .
     * Maybe a Support Library Bug .
     */

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });

    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}
class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    /**
     * Return fragment with respect to Position .
     */

    @Override
    public Fragment getItem(int position)
    {
        switch (position){
            case 0 : return new ImagesFragment();
            case 1 : return new ReviewFragment();

        }
        return null;
    }

    @Override
    public int getCount() {

        return int_items;

    }

    /**
     * This method returns the title of the tab according to the position.
     */

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position){
            case 0 :
                return "تصاویر";
            case 1 :
                return "نقد و بررسی";

        }
        return null;
    }
}
}

我希望这会有所帮助

答案 1 :(得分:0)

试试这个

 $em = $this->getDoctrine()->getManager();
 $sql = "SELECT p FROM AppBundle:Post WHERE p.staus = ?1 ORDER BY p.created DESC'";
 $query = $em->createQuery($sql);
 $query->setParameter(1, 'published');
 $result = $query->getResult();

有关详细信息,请参阅http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples